Spring Security OAuth 2.0:一、入门篇

论坛 期权论坛 脚本     
已经匿名di用户   2022-3-21 23:41   2891   0

入门篇:http://blog.didispace.com/spring-security-oauth2-xjf-1/原创链接写的不咋滴就是

对于刚入门的来说要好好理解以下以下三个继承类的作用 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分布式认证和授权方案【精】

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP