1

Ich habe eine App, mit der sich von oder von oauth2 alos einloggen konnte. Aber ich traf einige Probleme.Frühling Sicherheit oauth2.0 erholsam und Formular Login Config

Was ich getroffen:

  • Als ich http://127.0.0.1/ besuchen, es Truns was/Login-Seite ist richtig.
  • Wenn ich http://127.0.0.1/api/hellos besuche, truncht es auch auf/Login-Seite, was genau falsch ist. Was ich will ist, dass ich/api/hellos mit oauth2 zugreifen kann.

Hier ist meine Sicherheitskonfiguration:

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true) 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private SpringDataMyBatisUserDetailsService userDetailsService; 

    @Override 
    @Autowired 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth 
     .userDetailsService(this.userDetailsService) 
     .passwordEncoder(Manager.PASSWORD_ENCODER); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.addFilterBefore(new CorsFilter(), ChannelProcessingFilter.class); 
    } 

    @Configuration 
    @EnableAuthorizationServer 
    public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { 

     private final AuthenticationManager authenticationManager; 
     @Autowired 
     private TokenStore tokenStore; 
     @Autowired 
     private SpringDataMyBatisClientDetailsService clientDetailsService; 

     @Autowired 
     public AuthorizationServerConfig(AuthenticationManager authenticationManager) { 
      this.authenticationManager = authenticationManager; 
     } 

     /** 
     * Defines the security constraints on the token endpoints /oauth/token_key and /oauth/check_token 
     * Client credentials are required to access the endpoints 
     * 
     * @param oauthServer 
     * @throws Exception 
     */ 
     @Override 
     public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception { 
      oauthServer 
      .tokenKeyAccess("permitAll()") 
      .checkTokenAccess("isAuthenticated()"); 
     } 

     /** 
     * Defines the authorization and token endpoints and the token services 
     * 
     * @param endpoints 
     * @throws Exception 
     */ 
     @Override 
     public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { 
      endpoints 
      .authenticationManager(this.authenticationManager) 
      .tokenEnhancer(tokenEnhancer()) 
      .tokenStore(tokenStore); 
     } 

     @Override 
     public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
      clients 
      .withClientDetails(clientDetailsService); 
     } 

     @Bean 
     public TokenEnhancer tokenEnhancer() { 
      return new CustomTokenEnhancer(); 
     } 

    } 

    @Order(1) 
    @Configuration 
    public class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter { 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http 
      .csrf().disable() 
      .authorizeRequests() 
      .antMatchers("/index.html", "/index.css", "/common.js", "/index.js", "/api/**").permitAll() 
      .anyRequest().authenticated() 
      .and() 
      .formLogin() 
      .and() 
      .logout() 
      .logoutSuccessUrl("/") 
      .and().exceptionHandling().accessDeniedPage("/error/403"); 
     } 

    } 

    @Configuration 
    @EnableResourceServer 
    @Order(2) 
    public class AuthorizationResourceConfig extends ResourceServerConfigurerAdapter { 

     @Autowired 
     private RestAuthenticationEntryPoint restAuthenticationEntryPoint; 
     @Autowired 
     private AuthenticationSuccessHandler successHandler; 
     @Autowired 
     private AuthenticationFailureHandler failureHandler; 
     @Autowired 
     private TokenStore tokenStore; 

     @Override 
     public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 
      resources 
      .stateless(true) 
      .tokenStore(tokenStore); 
     } 

     @Override 
     public void configure(HttpSecurity http) throws Exception { 
      http 
      .authorizeRequests() 
      .and() 
      .anonymous().disable() 
      .sessionManagement() 
      .sessionCreationPolicy(SessionCreationPolicy.STATELESS) 
      .and().httpBasic() 
      .and() 
      .exceptionHandling() 
      .accessDeniedHandler(new OAuth2AccessDeniedHandler()) 
      .authenticationEntryPoint(restAuthenticationEntryPoint) 
      .and() 
      .authorizeRequests() 
      .antMatchers("/api/**").fullyAuthenticated(); 

     } 
    } 

} 

ich einige Möglichkeiten ausprobiert haben, wo ich von google.But keine gesucht kann mir helfen. Also, ich möchte wirklich jemand kann mir helfen, ich werde für Sie geschätzt werden.

Darüber hinaus ist die hilfreichste Information, die ich gesucht habe, this.

Antwort

0

Verwenden Sie diese ".antMatchers ("/api/** "). FullyAuthenticated();" ,, In diesem Fall benötigen Sie ein "LOG IN" für alle Aktionen/api/.... ,, if Sie wollen nicht "Login" Formular für "127.0.0.1/api/hellos" müssen Sie nur/api/ohne Sterne

+0

Ich bin nicht verwenden RESOURCE_PATH_MATCH .Ich habe es entfernt. – WhiteWater

+0

Verwenden Sie diese ".antMatchers ("/api/** "). FullyAuthenticated();" ,, In diesem Fall benötigen Sie ein "LOG IN" für alle Aktionen/api/.... ,, wenn Sie nicht wollen Anmeldeformular für "http://127.0.0.1/api/hellos" Sie müssen nur/api/ohne Sterne verwenden – kuciB

+0

Sie können das missverstehen. Mein Projekt besteht aus zwei verschiedenen Teilen, einem JSF-Admin-Panel und einem RESTfull-Service. Ich versuche, Spring-Sicherheit einzurichten, um verschiedene Authentifizierungsmethoden abhängig von der URL zu verwenden, die der Benutzer navigiert. – WhiteWater

Verwandte Themen