2017-01-05 1 views
-1

스프링 부트 앱에 두 가지 다른 로그인 흐름이 있습니다. 스프링 보안 다중 로그인 필터

  • 상태 비 로그인
    1. 일반 양식 로그인 나는 다음을 시도했지만 항상 2 하나 개의 필터에오고. 상태 비 저장 인증을 위해서는 /api/**을, 그리고 일반 세션 로그인에는 다른 것을 어떻게 제한합니까?

      @Configuration 
      public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
          @Autowired 
          DataSource dataSource; 
      
          @Autowired 
          public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception { 
           auth 
            .jdbcAuthentication() 
             .dataSource(dataSource) 
             .usersByUsernameQuery("select username,password, enabled from users where username=?") 
             .authoritiesByUsernameQuery("select username, authority from authorities where username=?"); 
          } 
      
          private final UserService userService; 
      
          private final TokenAuthenticationService tokenAuthenticationService; 
      
          public WebSecurityConfig() { 
           super(true); 
           this.userService = new UserService(); 
           tokenAuthenticationService = new TokenAuthenticationService("tooManySecrets", userService); 
          } 
      
          @Override 
          protected void configure(HttpSecurity http) throws Exception { 
           http 
            .csrf().disable(); 
      
           // Normal form login using jdbc 
           http 
            .authorizeRequests() 
             .antMatchers("/", "/home").access("hasRole('ROLE_ADMIN')") 
             .and() 
            .formLogin() 
             .loginPage("/login") 
             .usernameParameter("username") 
             .passwordParameter("password") 
             .permitAll() 
             .and() 
            .logout().permitAll(); 
           //Stateless authentication using tokens - Custome authentication from token implemented in the filter 
           http 
            .authorizeRequests() 
             .antMatchers("/api").authenticated() 
             .and() 
            .addFilterBefore(new StatelessAuthenticationFilter(tokenAuthenticationService), 
            UsernamePasswordAuthenticationFilter.class); 
          } 
      } 
      

    답변

    0

    전반적으로 스프링 보안은 다중 HTTP 블록을 사용하여이를 처리합니다.

    여러 구성을 중첩해야합니다. 이는 Java 구성에서이를 처리하는 방법입니다. 중요하게, 이러한 구성에는 "주문"주석이 있어야 올바른 순서대로 스프링을로드 할 수 있습니다. 특정 하위 경로 (예 : API)의 구성은 "/"가 포함 된 구성 전에로드해야합니다.

    @Configuration 
    public class WebSecurityConfig { 
    
        @Autowired 
        private DataSource dataSource; 
    
        private final UserService userService; 
    
        private final TokenAuthenticationService tokenAuthenticationService; 
    
        public WebSecurityConfig() { 
         super(true); 
         this.userService = new UserService(); 
         tokenAuthenticationService = new TokenAuthenticationService("tooManySecrets", userService); 
        } 
    
        @Order(0) 
        @Configuration 
        private final class ApiSecurity extends WebSecurityConfigurerAdapter { 
    
         @Override 
         protected void configure(HttpSecurity http) throws Exception { 
          http.csrf().disable(); 
    
          //Stateless authentication using tokens - Custome authentication from token implemented in the filter 
          http.antMatchers("/api").authorizeRequests() 
           .authenticated().and() 
           .addFilterBefore(new StatelessAuthenticationFilter(tokenAuthenticationService), 
          UsernamePasswordAuthenticationFilter.class); 
         } 
    
         @Autowired 
         public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception { 
          auth.jdbcAuthentication().dataSource(dataSource) 
          .usersByUsernameQuery("select username,password, enabled from users where username=?") 
          .authoritiesByUsernameQuery("select username, authority from authorities where username=?"); 
        } 
        } 
    
        @Order(Ordered.LOWEST_PRECEDENCE) 
        @Configuration 
        private final class OtherSecurity extends WebSecurityConfigurerAdapter { 
    
         @Override 
         protected void configure(HttpSecurity http) throws Exception { 
          http.csrf().disable(); 
    
          //Normal form login using jdbc 
          http.antMatchers("/", "/home").authorizeRequests().access("hasRole('ROLE_ADMIN')").and().formLogin() 
           .loginPage("/login") .usernameParameter("username") 
           .passwordParameter("password").permitAll().and().logout().permitAll(); 
         } 
    
         @Autowired 
         public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception { 
          auth.jdbcAuthentication().dataSource(dataSource) 
          .usersByUsernameQuery("select username,password, enabled from users where username=?") 
          .authoritiesByUsernameQuery("select username, authority from authorities where username=?"); 
         } 
        } 
    } 
    
    :

    그래서 당신의 예는이 비슷한 (그것을 간단하게 수 많은 재 배열, 단지 분할을 보여주는) 될 것