这些类之间有什么区别?我知道 WebSecurityConfigurerAdapter 用于自定义我们应用程序的“安全性”。
我做了什么:
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
CustomUserDetailsService customUserDetailsService;
@Autowired
private JwtAuthenticationEntryPoint unauthorizedHandler;
但是我不明白AuthorizationServerConfigurerAdapter的意思。
我读了几篇文章,但我不明白。
最佳答案
先说一件事。 OAuth 2 是一个授权框架。它允许应用程序(客户端)代表资源所有者(用户)获得对 HTTP 服务的有限访问权限。 OAuth 2 不是身份验证协议(protocol)。
AuthorizationServerConfigurerAdapter 用于配置OAuth 授权服务器的工作方式。
这里有一些可以配置的方面:
- 支持的授权类型(例如授权码授权)
- 授权码服务,用于存储授权码
- token 存储,用于存储访问和刷新 token (例如 JwtTokenStore)
- 客户端详细信息服务,保存客户端配置
- ...
WebSecurityConfigurerAdapter 用于配置如何保护 OAuth 授权服务器。
或者换句话说,用户必须如何进行身份验证才能授予客户端访问其资源的权限。
这可以是:
- 表单验证
- 通过身份提供商进行身份验证(Facebook 登录)
- ...
(我故意省略了一些细节,以使答案尽可能简单。)
具有内存中 token 存储的示例授权服务器配置:
@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();
}
...
}
关于java - AuthorizationServerConfigurerAdapter 与 WebSecurityConfigurerAdapter 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50919268/