单点登录概念

单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一。SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统。登录逻辑如上图

基于Spring 全家桶的实现

技术选型:

Spring Boot
Spring Cloud
Spring Security oAuth2

客户端:

maven依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-jwt</artifactId>
</dependency>

EnableOAuth2Sso 注解

入口类配置@@EnableOAuth2Sso


@SpringBootApplication
public class PigSsoClientDemoApplication { public static void main(String[] args) {
SpringApplication.run(PigSsoClientDemoApplication.class, args);
} }

配置文件

security:
oauth2:
client:
client-id: pig
client-secret: pig
user-authorization-uri: http://localhost:3000/oauth/authorize
access-token-uri: http://localhost:3000/oauth/token
scope: server
resource:
jwt:
key-uri: http://localhost:3000/oauth/token_key
sessions: never

SSO认证服务器

认证服务器配置

@Configuration
@Order(Integer.MIN_VALUE)
@EnableAuthorizationServer
public class PigAuthorizationConfig extends AuthorizationServerConfigurerAdapter { @Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient(authServerConfig.getClientId())
.secret(authServerConfig.getClientSecret())
.authorizedGrantTypes(SecurityConstants.REFRESH_TOKEN, SecurityConstants.PASSWORD,SecurityConstants.AUTHORIZATION_CODE)
.scopes(authServerConfig.getScope());
} @Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
endpoints
.tokenStore(new RedisTokenStore(redisConnectionFactory))
.accessTokenConverter(jwtAccessTokenConverter())
.authenticationManager(authenticationManager)
.exceptionTranslator(pigWebResponseExceptionTranslator)
.reuseRefreshTokens(false)
.userDetailsService(userDetailsService);
} @Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security
.allowFormAuthenticationForClients()
.tokenKeyAccess("isAuthenticated()")
.checkTokenAccess("permitAll()");
} @Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
} @Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
jwtAccessTokenConverter.setSigningKey(CommonConstant.SIGN_KEY);
return jwtAccessTokenConverter;
} }

一个Java交流平台分享给你们,让你在实践中积累经验掌握原理。如果你想拿高薪,想突破瓶颈,想跟别人竞争能取得优势的,想进BAT但是有担心面试不过的,可以加我的Java学习交流群:642830685

注:加群要求

1、大学学习的是Java相关专业,毕业后面试受挫,找不到对口工作

2、在公司待久了,现在过得很安逸,但跳槽时面试碰壁。需要在短时间内进修、跳槽拿高薪的

3、参加过线下培训后,知识点掌握不够深刻,就业困难,想继续深造

4、已经在Java相关部门上班的在职人员,对自身职业规划不清晰,混日子的

5、有一定的C语言基础,接触过java开发,想转行的

配置完成体验

  1. 访问SSO客户端的 index.html
  2. 重定向到SSO服务端的 Basic 认证
  3. 输入账号密码又重定向到原请求的 客户端index资源

总结

  • 客户端访问服务端 403问题? 用户需要拥有ROLE_USER的权限,具体的可以通过日志可以查看到报错。
  • Possible CSRF detected - state parameter was present but no state could be found 目前是通过设置session: never或者 cotext-path解决
  • 源码请参考 gitee.com/log4j/ 基于Spring Cloud、Spring Security Oauth2.0开发企业级认证与授权,提供常见服务监控、链路追踪、日志分析、缓存管理、任务调度等实现

最新文章

  1. Excel 锁定特定单元格 不允许更改
  2. LaTeX用dvi编译,Yap浏览器弹出对话框,决解办法(傻瓜教程)
  3. Zend studio 10.6 配置XDEBUG
  4. mysql多表查询例子
  5. python在linux上的GUI无法弹出界面
  6. 30.怎样在Swift中添加运行时属性?
  7. Spring的父子容器问题
  8. LeetCode 80
  9. Linux 网络I/O模型
  10. Windows 部署 Redis 群集(转)
  11. ScalaPB(1): using protobuf in akka
  12. Struct和Union在内存大小上的区别
  13. Linux进程管理工具Supervisor
  14. .NET Core 2.x中使用Named Options处理多个强类型配置实例
  15. asp.net 实现后台异步处理的方式
  16. Python全栈开发之路 【第十八篇】:Ajax技术
  17. 转:Linux(Centos)之安装Nginx及注意事项
  18. Struts2(接受表单参数)请求数据自动封装和数据类型转换
  19. hdu4300 Clairewd’s message 扩展KMP
  20. kotlin的安装(一)

热门文章

  1. [BZOJ2286][Sdoi2011]消耗战(虚树上DP)
  2. [CSP-S模拟测试]:统计(树状数组+乱搞)
  3. 基于ElementUI,设置流体高度时,固定列与底部有间隙
  4. Django日志的配置
  5. linux工作常用命令
  6. jsvnadmin访问一直登陆 找不到用户
  7. 设置iterm可配色
  8. 搜狗输入法弹窗搜狐新闻的处理 以及sogoucloud.exe的处理
  9. TODO C++ 高级篇
  10. centos7.7下docker与k8s安装(DevOps三)