Java Spring安全多重登录筛选器

Java Spring安全多重登录筛选器,java,spring,spring-boot,spring-security,Java,Spring,Spring Boot,Spring Security,我的Spring Boot应用程序中有两个不同的登录流 正常形式登录 无状态登录 我尝试了以下方法,但它总是出现在第二个过滤器上。如何将/api/**限制为无状态身份验证,将其他身份验证限制为正常会话表单登录 @配置 公共类WebSecurityConfig扩展了WebSecurityConfigureAdapter{ @自动连线 数据源数据源; @自动连线 public void configAuthenticationAuthenticationManagerBuilder身份验证引发异常{

我的Spring Boot应用程序中有两个不同的登录流

正常形式登录 无状态登录 我尝试了以下方法,但它总是出现在第二个过滤器上。如何将/api/**限制为无状态身份验证,将其他身份验证限制为正常会话表单登录

@配置 公共类WebSecurityConfig扩展了WebSecurityConfigureAdapter{ @自动连线 数据源数据源; @自动连线 public void configAuthenticationAuthenticationManagerBuilder身份验证引发异常{ 认证 .jdbc身份验证 .数据源数据源 .UsersByUserNameQuerySelectusername,password,从username=? .AuthoritiesByUserName查询选择用户名,用户名为?的权限?; } 专用最终用户服务用户服务; 专用最终TokenAuthenticationService TokenAuthenticationService; 公共网站安全配置{ 超真实; this.userService=新的userService; tokenAuthenticationService=新TokenAuthenticationServicetooManySecrets,userService; } @凌驾 受保护的void configureHttpSecurity http引发异常{ http .csrf.disable; //使用jdbc的标准表单登录 http .批准请求 .antMatchers/,/home.accesshasRole'ROLE\u ADMIN' 和 .formLogin .login页面/登录 .UserName参数UserName .passwordParameterpassword permitAll先生 和 .logout.permitAll; //使用令牌的无状态身份验证-来自在筛选器中实现的令牌的自定义身份验证 http .批准请求 .antMatchers/api.authenticated 和 .AddFilterBefor续订无状态身份验证FilterTokenauthenticationService, UsernamePasswordAuthenticationFilter.class; } }
总的来说,spring安全性通过使用多个HTTP块来处理这个问题

您将需要嵌套多个配置—这就是Java配置处理此问题的方式。重要的是,这些配置需要顺序注释以确保spring以正确的顺序加载它们-特定子路径(如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=?");
        } 
    }
}