5

मैं OAuth2 का उपयोग कर नमूना स्प्रिंग बूट ऐप पर काम कर रहा हूं। मुद्दा यह है कि ग्राहक localhost:8080 पर होस्ट https://localhost:8443/oauth/authorize के लिए बाहर कॉल ही (अंतर्निहित अनुदान प्रकार) को अधिकृत करने का लेकिन /oauth/authorize के बाद से की आवश्यकता है कि उपयोगकर्ता प्रमाणीकृत किया वे https://localhost:8443/login में प्रवेश पृष्ठ पर पुनः निर्देशित कर रहे है।स्प्रिंग ओएथ 2 फॉर्म लॉगिन पर क्लाइंट पर रीडायरेक्ट नहीं कर रहा है

यह सभी की उम्मीद है, लेकिन जब प्रवेश पृष्ठ पर उपयोगकर्ता भूमि redirect_uri सहित क्वेरी स्ट्रिंग के सभी याद कर रहे हैं। उपयोगकर्ता http://localhost:8080 के निर्दिष्ट रीडायरेक्ट_री के बजाय https://localhost:8443 पर लॉग इन करता है और उसे रीडायरेक्ट किया जाता है।

वहाँ ग्राहक पर वापस पुनर्निर्देशित उपयोगकर्ता/सर्वर का प्रवेश फार्म w में प्रवेश करने के बाद करने के लिए कोई रास्ता है? क्या मैं अपनी कॉन्फ़िगरेशन में कुछ खो रहा हूं? मैं आवश्यकतानुसार अधिक पोस्ट कर सकता हूं। https://localhost:8443/oauth/authorize?response_type=token&state=6c2bb162-0f26-4caa-abbe-b65f7e5c6a2e&redirect_uri=http%3A%2F%2Flocalhost%3A8080&client_id=admin

SecurityConfig:

@Configuration 
public static class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    private final Logger log = LoggerFactory.getLogger(WebSecurityConfig.class);       

    @Override 
    public void configure(WebSecurity web) throws Exception { 

     web.ignoring().antMatchers("/resources/**"); 
    }  

    @SuppressWarnings("deprecation") 
    @Override 
    protected void configure(HttpSecurity http) throws Exception {     

     http 
      .requestMatchers() 
       .antMatchers("/**") 
     .and() 
      .addFilterAfter(new CsrfCookieGeneratorFilter(), CsrfFilter.class) 
      .exceptionHandling() 
       .accessDeniedPage("/login?authorization_error=true") 
     .and() 
      .authorizeRequests() 
      .antMatchers("/resources/**", "/csrf").permitAll() 
      .anyRequest().authenticated() 
     .and() 
      .formLogin() 
       .loginPage("/login") 
       .usernameParameter("j_username") 
       .passwordParameter("j_password") 
       .defaultSuccessUrl("/", false) 
       .failureUrl("/login?authentication_error=true") 
       .permitAll() 
     .and() 
      .logout() 
       .logoutRequestMatcher(new AntPathRequestMatcher("/logout")) 
       .logoutSuccessUrl("/login") 
       .invalidateHttpSession(true) 
       .deleteCookies("JSESSIONID", "CSRF-TOKEN") 
       .permitAll() 
     .and() 
      .headers() 
       .frameOptions() 
       .disable(); 
    } 

OAuthConfig:

@Configuration 
@EnableAuthorizationServer 
protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter { 

    @Inject 
    @Qualifier("authenticationManagerBean") 
    private AuthenticationManager authenticationManager; 

    @Bean 
    public TokenStore tokenStore() { 

     return new InMemoryTokenStore(); 
    } 

    @Primary 
    @Bean 
    public ResourceServerTokenServices tokenServices() { 

     DefaultTokenServices tokenServices = new DefaultTokenServices(); 
     tokenServices.setSupportRefreshToken(true); 
     tokenServices.setTokenStore(tokenStore()); 

     return tokenServices; 
    }   

    @Bean 
    public ApprovalStore approvalStore() throws Exception { 

     TokenApprovalStore approvalStore = new TokenApprovalStore(); 
     approvalStore.setTokenStore(tokenStore()); 

     return approvalStore; 
    } 

    @Override 
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 

     clients 
      .inMemory() 
       .withClient("read-only") 
        .secret("readme") 
        .resourceIds(RESOURCE_ID) 
        .authorizedGrantTypes("implicit", "password", "refresh_token")       
        .authorities(Constant.USER) 
        .scopes("read") 
        .autoApprove(true) 
        .redirectUris("https://localhost:8443") 
       .and() 
       .withClient("admin") 
        .secret("admin") 
        .resourceIds(RESOURCE_ID) 
        .authorizedGrantTypes("implicit", "password", "refresh_token") 
        .authorities(Constant.USER, Constant.ADMIN) 
        .scopes("read", "write") 
        .autoApprove(true) 
        .redirectUris("https://localhost:8443", "http://localhost:8080") 
       .and() 
       .withClient("super-admin") 
        .secret("super") 
        .resourceIds(RESOURCE_ID) 
        .authorizedGrantTypes("implicit", "password", "refresh_token") 
        .authorities(Constant.USER, Constant.ADMIN) 
        .scopes("read", "write", "delete") 
        .redirectUris("https://localhost:8443"); 
    } 

    @Override 
    public void configure(AuthorizationServerEndpointsConfigurer configurer) throws Exception { 

     configurer 
      .tokenStore(tokenStore()) 
      .authenticationManager(authenticationManager); 
    }   

    @Override 
    public void configure(AuthorizationServerSecurityConfigurer security) 
      throws Exception { 

     security.realm("hubble/client"); 
    } 

} 

@Configuration 
@EnableResourceServer 
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {   

    @Override 
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 

     resources.resourceId(RESOURCE_ID); 
    } 

    @Override 
    public void configure(HttpSecurity http) throws Exception { 

     http 
      .requestMatchers() 
       .antMatchers("/api/**") 
     .and() 
      .authorizeRequests() 
       .antMatchers(HttpMethod.OPTIONS, "/api/**").permitAll() 
       .antMatchers(HttpMethod.GET, "/api/**").access("#oauth2.hasScope('read')") 
       .antMatchers(HttpMethod.POST, "/api/**").access("#oauth2.hasScope('write')") 
       .antMatchers(HttpMethod.PATCH, "/api/**").access("#oauth2.hasScope('write')") 
       .antMatchers(HttpMethod.PUT, "/api/**").access("#oauth2.hasScope('write')") 
       .antMatchers(HttpMethod.DELETE, "/api/**").access("#oauth2.hasScope('delete')") 
       .antMatchers("/api/**").access("hasRole('" + Constant.USER + "')")     
     .and() 
      .anonymous().authorities(Constant.ANONYMOUS) 
     .and() 
      .sessionManagement() 
       .sessionCreationPolicy(SessionCreationPolicy.STATELESS);       
    } 
} 

@Configuration 
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true) 
protected static class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {  

    @Override 
    protected MethodSecurityExpressionHandler createExpressionHandler() { 

     OAuth2MethodSecurityExpressionHandler methodHandler = new OAuth2MethodSecurityExpressionHandler(); 

     return methodHandler; 
    } 
} 
+0

यह मेरे लिए काम करता है (मैं का उपयोग 'defaultSuccessUrl कभी नहीं()' लेकिन मैं उस के साथ किसी भी स्पष्ट समस्या नहीं दिख रहा है) । क्या आप एक पूर्ण परियोजना के लिए एक लिंक पोस्ट कर सकते हैं? –

+0

धन्यवाद @ डेवसेयर एक नज़र डालने के लिए! मैंने डिफ़ॉल्ट SuccessUrl() को उम्मीद की है कि यह मेरी समस्या को हल करेगी, लेकिन बिना पासा के या बिना पासा। यह काम कर रहा है अगर मैं फॉर्म लॉगिन के बजाय बुनियादी लेख के साथ/oauth/** की रक्षा करता हूं। मैं कल जिथब पर परियोजना को आजमाउंगा और एक लिंक प्रदान करूंगा। –

+3

हाय, इस के साथ कोई सफलता? मेरे साथ भी वही दिक्कत है। – pborbas

उत्तर

0

समस्या प्रपत्र प्रमाणीकरण के साथ ही होता है और यह OAuth से संबंधित नहीं है की तरह

अधिकृत अनुरोध लग रहा है। org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint एक buildRedirectUrlToLoginPage विधि जिसे प्रवेश यूआरएल बनाता है और यह क्वेरी स्ट्रिंग भूल जाती है।

वर्तमान में हमने इसे एक समाधान के साथ हल किया।

  1. उपयोगकर्ता को प्राधिकृत यूआरएल को रीडायरेक्ट करने के बजाय, हम सीधे लॉगिन पृष्ठ पर रीडायरेक्ट करते हैं।
  2. प्रवेश पृष्ठ और एक नियंत्रक यदि उपयोगकर्ता पहले से प्रवेश किया गया था की जाँच करता है कि है यदि ऐसा है तो यह redirect_uri मौजूद होने पर या redirect_uri के रूप में डिफ़ॉल्ट एप्लिकेशन यूआरएल के साथ अधिकृत URL पर रीडायरेक्ट।
  3. यहां से redirect_uri प्राधिकृत यूआरएल द्वारा सही तरीके से संभाला जाता है।

कदम 2. से एक उदाहरण LoginController ऐसा दिखाई दे सकता:

@Controller 
@RequestMapping(value = {"/login"}) 
public class LoginController { 
    @RequestMapping(method = RequestMethod.GET) 
    public String getPage(HttpServletRequest request, HttpServletResponse response, Principal principal) 
      throws IOException { 
     if (principal != null) { //depends on your security config, maybe you want to check the security context instead if you allow anonym access 
      String redirect_uri = request.getParameter("redirect_uri"); 
      //here you must get all the other attributes thats needed for the authorize url 
      if (redirect_uri == null) { 
       redirect_uri = "https://your.default.app.url"; 
      }   
      return "redirect:https://localhost:8443/oauth/authorize?response_type=token&state=6c2bb162-0f26-4caa-abbe-b65f7e5c6a2e&client_id=admin&redirect_uri=" + URLEncoder.encode(redirect_uri, "UTF-8"); 
     } 
     return "login"; 
    } 
} 
संबंधित मुद्दे