SpringBoot security 整合 OAuth2

2019-02-20  本文已影响0人  yyq唯心不易

从零开始的Spring Security Oauth2(一)

详细介绍springboot security 结合oauth2 的代码

补充

刷新token:post请求

http://localhost:8080/oauth/token?grant_type=refresh_token&client_id=client_2&client_secret=123456&refresh_token=a1ff1fc4-46ea-49b6-8925-7ec53054ac75

设置token有效时间

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        //配置两个客户端,一个用于password认证一个用于client认证
        clients.inMemory().withClient("client_1")
                .resourceIds(DEMO_RESOURCE_ID)
                .authorizedGrantTypes("client_credentials", "refresh_token")
                .scopes("select")
                .authorities("client")
                .secret("123456")
                .and().withClient("client_2")
                .resourceIds(DEMO_RESOURCE_ID)
                .authorizedGrantTypes("password", "refresh_token")
                .scopes("select")
                .authorities("client")
                .secret("123456")
                //accessToken 设置有效期
                .accessTokenValiditySeconds(1800)
                //refreshToken 设置有效期
                .refreshTokenValiditySeconds(3600000);
    }
遇到的坑:
  1. AuthenticationManager 注入不了

    解决办法:在 WebSecurityConfig 写入
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

  1. There is no PasswordEncoder mapped for the id "null"

    解决办法:没有注入passwordEncoder,自己写个类继承,或者使用BCryptPasswordEncoder
    /**
     * 采用自己的密码匹配方式
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new MyPasswordEncoder();
    }

public class MyPasswordEncoder implements PasswordEncoder {
    @Override
    public String encode(CharSequence rawPassword) {
        return rawPassword.toString();
    }

    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        return encodedPassword.equals(rawPassword.toString());
    }
}

3.NoSuchMethodError: org.springframework.data.redis.connection.RedisConnection.set([B[B)V

解决方法:maven引入的版本问题,redis和oauth的版本没有兼容。

    <!--springboot 使用版本-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/>
    </parent>
    
    <dependency>
        <groupId>org.springframework.security.oauth</groupId>
        <artifactId>spring-security-oauth2</artifactId>
        <version>2.3.2.RELEASE</version>
    </dependency>
  1. 使用refresh_token,返回{"error":"server_error","error_description":"UserDetailsService is required."}

解决办法:

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints
                //采用redis存储token信息
                .tokenStore(new RedisTokenStore(redisConnectionFactory))
                .authenticationManager(authenticationManager)
                //解决reflush token 报错问题
                .userDetailsService(userDetailsService);
    }

从零开始的Spring Security Oauth2(二)

分析源码:获取token的流程

从零开始的Spring Security Oauth2(三)

分析源码:携带token访问受限资源时,内部的工作流程

上一篇下一篇

猜你喜欢

热点阅读