账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    spring security和oauth2的资源控制互相覆盖,无法同时生效
    29
    0

    在本来spring security的基础上使用了spring security oauth2,控制/api下的请求。浏览了很多网上的配置,但是测试时发现spring security的资源控制和spring securtiy oauth2的资源控制会互相覆盖,没法做到分离控制。如果配置添加了security.oauth2.resource.filter-order=3,则使用spring security的控制,反之则为oauth2的控制。

    代码中我的配置如下:

    Spring security配置:

    @Configuration
    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
        @Autowired
        private UserManagerService userManagerService;
        
        @Override
        @Bean //分享到oauth2
        public AuthenticationManager authenticationManagerBean() throws Exception {
            return super.authenticationManagerBean();
        }
        
        /**
         * 密码加密
         */
        @Bean
        public BCryptPasswordEncoder passwordEncoder(){
            return new BCryptPasswordEncoder();
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                // 关闭csrf保护功能(跨域访问)
                .csrf().disable()
                .authorizeRequests()
                    .antMatchers("/oauth/**").permitAll()
                    .antMatchers("/**/*.js", "/**/*.css", "/**/*.png",
                            "/**/*.gif", "/**/*.jpg", "/**/*.jpeg", "/**/*.map",
                            "/**/*.ico").permitAll()
                    .anyRequest().authenticated()
                    .and()
                .formLogin()
                    .loginPage("/user/login_page")
                    .loginProcessingUrl("/login")
                    .usernameParameter("username")
                    .passwordParameter("password")
                    .successHandler(new CustomSimpleUrlAuthenticationSuccessHandler())
                    .failureHandler(new CustomSimpleUrlAuthenticationFailureHandler())
                    .permitAll()
                     .and()
                 .logout()
                     .logoutUrl("/logout")
                     .logoutSuccessUrl("/user/login_page")
                     .permitAll();
        }
        
        @Override
        protected void configure(AuthenticationManagerBuilder auth)
                throws Exception {
            auth.userDetailsService(userManagerService)
            .passwordEncoder(passwordEncoder());
        }
        
    }

    Spring security oatuth2配置:

    @Configuration
    @EnableAuthorizationServer
    public class AuthorizationServerConfiguration
            extends AuthorizationServerConfigurerAdapter {
        @Autowired
        AuthenticationManager authenticationManager;
        @Autowired
        private UserManagerService userManagerService;
        
        @Bean
        public TokenStore tokenStore() {
            return new InMemoryTokenStore();
        }
    
        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints)
                throws Exception {
            endpoints.tokenStore(tokenStore())
                    .userDetailsService(userManagerService)
                    .authenticationManager(authenticationManager);
        }
    
        @Override
        public void configure(AuthorizationServerSecurityConfigurer security)
                throws Exception {
            // 允许表单认证
            security
                    .allowFormAuthenticationForClients();
        }
    
        @Override
        public void configure(ClientDetailsServiceConfigurer clients)
                throws Exception {
            clients.inMemory()
                    .withClient("cmdb")
                    .authorizedGrantTypes("password", "refresh_token")
                    .secret("api")
                    .scopes("xxx");
        }
    }
    
    @Configuration
    @EnableResourceServer
    public class ResourceServerConfiguration
            extends ResourceServerConfigurerAdapter {
        @Override
        public void configure(HttpSecurity http) throws Exception {
            http
                    .authorizeRequests()
                    .antMatchers("/api/**").authenticated();
        }
    }

    之前查阅过很多博客,也查过spring oauth2的几种模式的授权流程,但是都没有找到原因

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 回憶↘無止境的痛 普通会员 1楼

      Spring Security和OAuth2在许多方面都有相似之处,但它们在实现方式上有所不同。

      首先,Spring Security是基于Spring框架的,它使用Spring Security核心,提供了许多安全功能,如身份验证、授权、日志管理等。而OAuth2是独立于Spring框架的,它是一种授权框架,允许客户端和服务器之间的身份验证和授权。

      其次,Spring Security和OAuth2在实现方式上有所不同。Spring Security主要通过用户认证和授权功能实现资源控制,而OAuth2则主要通过访问令牌(Access Token)实现资源控制。

      此外,Spring Security和OAuth2在使用场景上也有所不同。Spring Security主要用于web应用,而OAuth2则主要用于移动应用和企业应用。

      因此,虽然Spring Security和OAuth2在实现方式上有所不同,但它们在许多方面都有相似之处,如实现资源控制。如果你需要同时使用Spring Security和OAuth2,建议你按照OAuth2的使用场景进行配置。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部