|
对于刚入门的来说要好好理解以下以下三个继承类的作用 WebSecurityConfigurerAdapter、ResourceServerConfigurerAdapter、AuthorizationServerConfigurerAdapter
这篇文章是不错,但是有几处bug,在此补充,以儆效尤
1.1、IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
解决方案:添加 MyPasswordEncoder 类(注意要添加 @Component 注解,启动程序的时候最好用idea原生的,我就在JRebel中吃了亏)
@Component
public class MyPasswordEncoder implements PasswordEncoder {
@Override
public String encode(CharSequence charSequence) {
return charSequence.toString();
}
@Override
public boolean matches(CharSequence charSequence, String s) {
return s.equals(charSequence.toString());
}
}
重新请求: http://localhost:8080/oauth/token?grant_type=client_credentials&scope=select&client_id=client_1&client_secret=123456
1.2、{"error":"unsupported_grant_type","error_description":"Unsupported grant type: password"}
解决方案:意思是没有定义password的模式,需要在 WebSecurityConfigurerAdapter 的实现类中添加以下代码
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
从新请求: http://localhost:8080/oauth/token?username=user_1&password=123456&grant_type=password&scope=select&client_id=client_2&client_secret=123456
1.3、oauth2验证中WebSecurityConfigurerAdapter和ResourceServerConfigurerAdapter的区别
WebSecurityConfigurerAdapter默认情况下是springsecurity的http配置 ResourceServerConfigurerAdapter默认情况下是spring security oauth2的http配置
但是ResourceServerConfigurerAdapter的默认order是大于100的,意思是WebSecurityConfigurerAdapter的拦截顺序要先于ResourceServerConfigurerAdapter,我们想要使用oauth的http配置,就需要在相应的配置文件(yml)配置security.oauth2.resource.filter-order = 3 这样就让ResourceServerConfigurerAdapter的生效
其它入门文档:
1、理解OAuth 2.0 阮一峰
2、Re:从零开始的Spring Security Oauth2(一)
3、Spring Security 与 OAuth2(完整案例)
4、Spring Security 解析(五) —— Spring Security Oauth2 开发
5、Spring Security OAuth2.0分布式认证和授权方案【精】
|