V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Buffer2Disk
V2EX  ›  程序员

关于 spring security 的一个问题

  •  
  •   Buffer2Disk · 2019-08-31 23:25:30 +08:00 · 1403 次点击
    这是一个创建于 1693 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码如下,启用了两个不同的 HttpSecurity,分别为管理员和普通用户的

    按照官方的文档的说明,设置了 order 优先级后,如果路径在普通用户这里没匹配到,那么就会去管理员那边匹配。

    实际情况是,

    @Order(1) 设置到了用户这边,管理员的路径就不会被拦截(没有去请求 login/admin);

    @Order(1) 设置到了管理员那边,用户这边的路径就不会被拦截(没有去请求 login/user)

    这是什么原因呢?

    官方文档如下 https://docs.spring.io/spring-security/site/docs/4.2.13.BUILD-SNAPSHOT/reference/htmlsingle/#multiple-httpsecurity

    @EnableWebSecurity
    public class MultiHttpSecurityConfig {
    
        /**
         * 普通用户路径的拦截
         */
        @Configuration
        @Order(1)
        public static class UserWebSecurityConfig extends WebSecurityConfigurerAdapter {
            @Autowired
            CustomAuthenticationSuccessHandler successHandler;
    
            @Autowired
            CustomAuthenticationFailureHandler failureHandler;
    
            @Autowired
            private CustomAuthenticationProvider customAuthProvider;
    
            @Autowired
            private CustomUserDetailsService userDetailsService;
    
            @Value("${my.cookie.timeout}")
            private int cookieTimeOut;
    
    
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http.csrf().disable();
                http.authorizeRequests()
                    .antMatchers("/css/**", "/js/**", "/images/**, /fonts/**").permitAll()
                    .antMatchers("/bbb/**","/aaaa/**").hasAnyRole("USER");
                http.formLogin()
                    .successHandler(successHandler)
                    .failureHandler(failureHandler)
                    .loginPage("/login/user").permitAll();
                http.logout().permitAll();
    
                http.rememberMe().key("uniqueAndSecret").tokenValiditySeconds(cookieTimeOut);
            }
    
            @Override
            protected void configure(AuthenticationManagerBuilder auth) throws Exception {
                auth.authenticationProvider(customAuthProvider);
                auth.userDetailsService(userDetailsService);
            }
        }
    
    
        /**
         * 管理员路径的拦截
         */
        @Configuration
        public static class AdminWebSecurityConfig extends WebSecurityConfigurerAdapter {
            @Autowired
            CustomAuthenticationSuccessHandler successHandler;
    
            @Autowired
            CustomAuthenticationFailureHandler failureHandler;
    
            @Value("${my.cookie.timeout}")
            private int cookieTimeOut;
    
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http.csrf().disable();
                http.authorizeRequests()
                    .antMatchers("/css/**", "/js/**", "/images/**, /fonts/**").permitAll()
                    .antMatchers("/ccc/**","/dddd").hasAnyRole("ADMIN");
                http.formLogin()
                    .successHandler(successHandler)
                    .failureHandler(failureHandler)
                    .loginPage("/login/admin").permitAll();
                http.logout().permitAll();
    
                http.rememberMe().key("uniqueAndSecret").tokenValiditySeconds(cookieTimeOut);
            }
    
            @Autowired
            public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
                auth.inMemoryAuthentication()
                    .withUser("test").password("test").roles("ADMIN");
            }
        }
    }
    
    
    Buffer2Disk
        1
    Buffer2Disk  
    OP
       2019-09-01 11:26:24 +08:00
    顶一个
    JamesMackerel
        2
    JamesMackerel  
       2019-09-01 21:02:40 +08:00 via iPhone
    放弃吧,别用 security,自己写 aop,快多了……
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1184 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 18:15 · PVG 02:15 · LAX 11:15 · JFK 14:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.