Spring Boot 2.0 整合 Spring Security Oauth2

是金子在哪都会发光的——每一个说这句话的人都误觉得本身是金子。html

https://raw.githubusercontent.com/longfeizheng/longfeizheng.github.io/master/images/spring-security-OAuth205.png
https://user-gold-cdn.xitu.io/2018/4/29/16311778866b1c3b?w=2199&h=1500&f=jpeg&s=385604

前言

Spring Security源码分析十一:Spring Security OAuth2整合JWT中,咱们使用Spring Boot 1.5.6.RELEASE版本整合Spring Security Oauth2实现了受权码模式、密码模式以及用户自定义登陆返回token。但更新至Spring Boot 2.0.1.RELEASE版本时会出现一些小问题。在此,帮你们踩一下坑。关于OAuth2请参考理解OAuth 2.0java

修改pom.xml

更新Spring Boot版本为Spring Boot 2.0.1.RELEASEgit

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
复制代码

新增SecurityConfig配置

新增SecurityConfig用于暴露AuthenticationManagergithub

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        AuthenticationManager manager = super.authenticationManagerBean();
        return manager;
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
// .formLogin().and()
                .httpBasic().and()
                .csrf().disable();
    }
}
复制代码

修改MerryyouAuthorizationServerConfig

修改MerryyouAuthorizationServerConfig用于加密clientsecret和设置重定向地址redis

......
 @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        InMemoryClientDetailsServiceBuilder build = clients.inMemory();
        if (ArrayUtils.isNotEmpty(oAuth2Properties.getClients())) {
            for (OAuth2ClientProperties config : oAuth2Properties.getClients()) {
                build.withClient(config.getClientId())
                        .secret(passwordEncoder.encode(config.getClientSecret()))
                        .accessTokenValiditySeconds(config.getAccessTokenValiditySeconds())
                        .refreshTokenValiditySeconds(60 * 60 * 24 * 15)
                        .authorizedGrantTypes("refresh_token", "password", "authorization_code")//OAuth2支持的验证模式
                        .redirectUris("http://www.merryyou.cn")
                        .scopes("all");
            }
        }
......
复制代码

修改application.yml

因为在2.x版本中因为引入了不一样的客户端,须要指定配置哪一种链接池。spring

server:
 port: 8888
 redis:
 host: localhost
 port: 6379
 jedis:
 pool:
 max-active: 8
 max-wait: -1
 min-idle: 0
 max-idle: 8
logging:
 level:
    org.springframework: info
merryyou:
 security:
 oauth2:
 storeType: redis #或者jwt
 jwtSigningKey: merryyou
      clients[0]:
 clientId: merryyou
 clientSecret: merryyou
      clients[1]:
 clientId: merryyou1
 clientSecret: merryyou1

复制代码

效果以下

受权码模式

https://user-gold-cdn.xitu.io/2018/4/29/1631177886679360?w=1818&h=849&f=gif&s=1014813
https://user-gold-cdn.xitu.io/2018/4/29/1631177886679360?w=1818&h=849&f=gif&s=1014813

密码模式

https://user-gold-cdn.xitu.io/2018/4/29/16311778869cd3fd?w=1818&h=849&f=gif&s=605934
https://user-gold-cdn.xitu.io/2018/4/29/16311778869cd3fd?w=1818&h=849&f=gif&s=605934

自定义登陆

https://user-gold-cdn.xitu.io/2018/4/29/1631177885d342ad?w=1818&h=849&f=gif&s=250128
https://user-gold-cdn.xitu.io/2018/4/29/1631177885d342ad?w=1818&h=849&f=gif&s=250128

刷新token

https://user-gold-cdn.xitu.io/2018/4/29/1631177885e2eb7b?w=1818&h=849&f=gif&s=1105517
https://user-gold-cdn.xitu.io/2018/4/29/1631177885e2eb7b?w=1818&h=849&f=gif&s=1105517

代码下载

参考

推荐文章

  1. Java建立区块链系列
  2. Spring Security源码分析系列
  3. Spring Data Jpa 系列
  4. 【译】数据结构中关于树的一切(java版)
  5. SpringBoot+Docker+Git+Jenkins实现简易的持续集成和持续部署

https://user-gold-cdn.xitu.io/2018/4/29/1631177886939260?w=301&h=330&f=png&s=78572
https://user-gold-cdn.xitu.io/2018/4/29/1631177886939260?w=301&h=330&f=png&s=78572

🙂🙂🙂关注微信小程序java架构师历程 上下班的路上无聊吗?还在看小说、新闻吗?不知道怎样提升本身的技术吗?来吧这里有你须要的java架构文章,1.5w+的java工程师都在看,你还在等什么?小程序

相关文章
相关标签/搜索