1

私はoauth2 alosからログインできるアプリを持っています。 しかし、私はいくつかの問題に会った。春のセキュリティoauth2.0安心してフォームのログイン設定

私が会った:私はhttp://127.0.0.1/にアクセスすると

  • することは、それが正しいことを/ログインページにtruns。
  • http://127.0.0.1/api/hellosにアクセスすると、/ loginページに間違っているものも表示されます。私が望むのは、oauth2を使って/ api/hellosにアクセスできることです。私は私を助けることができるgoogle.Butなしから検索いくつかの方法を試してみました

    @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(); 
    
         } 
        } 
    
    } 
    

は、ここに私のセキュリティの設定です。 だから、私は本当に誰かが私を助けてほしい、私はあなたのために評価されます。

さらに、私が検索した最も有用な情報はthisです。

答えて

0

この「.antMatchers( "/ api/**")。fullyAuthenticated(); "を使用すると、この場合、すべてのアクションの" LOG IN "が必要になります。あなたは「127.0.0.1/api/hellos」の「ログイン」フォームを使用します。スターなしで/ api/onlyを使用する必要があります。

+0

私はRESOURCE_PATH_MATCHを使用していません。 – WhiteWater

+0

あなたはこの ".antMatchers("/api/** ")。fullyAuthenticated();"を使用します。この場合、すべてのアクション/ api/....に対して "LOG IN"が必要です。 "http://127.0.0.1/api/hellos"の "ログイン"フォームの場合は、/ api/starsのみを使用する必要があります。 – kuciB

+0

誤解を招く可能性があります。私のプロジェクトは、JSF管理パネルとRESTfullサービスという2つの異なる部分を公開しています。私は、ユーザーがナビゲートするURLに応じて異なる認証方法を使用するようにスプリングセキュリティを設定しようとしています。 – WhiteWater

関連する問題