【问题标题】:Difference between AuthorizationServerConfigurerAdapter vs WebSecurityConfigurerAdapterAuthorizationServerConfigurerAdapter 与 WebSecurityConfigurerAdapter 之间的区别
【发布时间】:2018-11-27 21:04:35
【问题描述】:

这些类之间有什么区别?我知道 WebSecurityConfigurerAdapter 用于自定义我们应用的“安全性”。

我做了什么:

public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
CustomUserDetailsService customUserDetailsService;

@Autowired
private JwtAuthenticationEntryPoint unauthorizedHandler;

但我不明白 AuthorizationServerConfigurerAdapter 的含义。

我读了几篇文章,但我不明白。

【问题讨论】:

  • 你使用 OAuth2 吗?你有什么不明白?你已经明白了什么?你读了哪些文章?
  • 是的,我正在使用 oauth。我不明白授权服务器配置适配器的目的

标签: java spring spring-boot spring-security


【解决方案1】:

如果要使用第三方身份验证器,即同时使用 OAuth,则必须在 OAuth 服务器端使用 AuthorizationServerConfigurerAdapter 和 WebSecurityConfigurerAdapter。如果不是这样,WebSecurityConfigurerAdapter 普通认证就足够了

【讨论】:

    【解决方案2】:

    先做一件事。 OAuth 2 是一个授权框架。它允许应用程序(客户端)代表资源所有者(用户)获得对 HTTP 服务的有限访问。 OAuth 2 不是身份验证协议。

    AuthorizationServerConfigurerAdapter 用于配置OAuth 授权服务器的工作方式

    以下是一些可以配置的方面:

    • 支持的授权类型(例如授权码授权)
    • 授权码服务,存储授权码
    • 令牌存储,用于存储访问和刷新令牌(例如 JwtTokenStore)
    • 客户端详细信息服务,保存客户端配置
    • ...

    WebSecurityConfigurerAdapter 用于配置如何保护 OAuth 授权服务器

    或者换句话说,用户必须如何进行身份验证才能授予客户端对其资源的访问权限。

    这可以是:

    • 表单认证
    • 通过身份提供商进行身份验证(Facebook 登录)
    • ...

    (我故意省略了一些细节以使答案尽可能简单。)


    具有内存令牌存储的示例授权服务器配置:

    @Configuration
    @EnableAuthorizationServer
    public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
    
        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
            endpoints.tokenStore(tokenStore());
        }
    
        @Bean
        public TokenStore tokenStore() {
            return new InMemoryTokenStore();
        }
    
        ...
    
    }
    

    使用表单登录的安全配置示例:

    @Configuration
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
                    .antMatchers("/login").permitAll()
                    .antMatchers("/oauth/authorize").authenticated()
                    .and()
                .formLogin();
        }
    
        ...
    
    }
    

    【讨论】:

    • 很简单的解释。不错。
    猜你喜欢
    • 2018-09-13
    • 2019-04-09
    • 2014-10-30
    • 2021-12-07
    • 2016-02-09
    • 2016-05-14
    • 2016-10-11
    • 2015-06-02
    • 2018-04-08
    相关资源
    最近更新 更多