博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot 2.0 整合 Spring Security Oauth2
阅读量:7014 次
发布时间:2019-06-28

本文共 2720 字,大约阅读时间需要 9 分钟。

hot3.png

是金子在哪都会发光的——每个说这句话的人都误以为自己是金子。

前言

在中,我们使用Spring Boot 1.5.6.RELEASE版本整合Spring Security Oauth2实现了授权码模式、密码模式以及用户自定义登录返回token。但更新至Spring Boot 2.0.1.RELEASE版本时会出现一些小问题。在此,帮大家踩一下坑。关于OAuth2请参考

修改pom.xml

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

org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE

新增SecurityConfig配置

新增SecurityConfig用于暴露AuthenticationManager

@Configurationpublic 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和设置重定向地址

...... @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版本中由于引入了不同的客户端,需要指定配置哪种连接池。

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

效果如下

授权码模式

密码模式

自定义登录

刷新token

代码下载

  • github:
  • gitee:

参考

推荐文章


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

转载于:https://my.oschina.net/merryyou/blog/1803206

你可能感兴趣的文章
Android中文API(130) —— Html
查看>>
jQuery调用WCF需要注意的一些问题(转dudu)
查看>>
Sql Server2012 Reporting Services 配置管理器 (SSRS)--不支持“sharepint集成”模式选择项...
查看>>
[转载]CharSequence类型
查看>>
IOS(数据库的应用)
查看>>
biztalk 2010 映射
查看>>
hdu 2144(LCS+并查集)
查看>>
JavaScript中逗号运算符
查看>>
盘点武汉十大宜居地段
查看>>
LeetCode题解:Rotate List
查看>>
进程关系之孤儿进程组
查看>>
Foundation框架—字符串
查看>>
数据库设计中的14个技巧
查看>>
7月份文章回顾
查看>>
25个顶级的jQuery表格插件
查看>>
memcached命令行操作详解,命令选项的详细解释
查看>>
Linux pipe函数
查看>>
java msgbox
查看>>
发布两款JQ小插件(图片查看器 + 分类选择器),开源
查看>>
linux中的namespace
查看>>