赞
踩
活动地址:CSDN21天学习挑战赛
单点登录:Single Sign On(简称SSO)
以前单系统时代,所有功能都在一起,登录一次就可以访问所有功能,随着业务的发展,系统越来越庞大,为了对合理的利用资源以及减少模块这间的耦合度,现在一般会把系统拆成不同的服务,或拆分成各个子系统。为了方便,各个子系统会互相授权,也就是只要在一个子系统上登录,那么在访问其它子系统时,就不需要在登录了。也就是说用户只要登录一次,就可以访问各个子系统,这就是单点登录。
代码来源胖哥:https://gitee.com/felord/spring-security-oauth2-tutorial
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> </dependencies>
application.yaml
application-multi.yaml
/** * @author felord.cn */ @EnableWebSecurity(debug = true) public class UserDetailsServiceConfiguration { /** * 这里虚拟一个用户 felord 123456 随机密码 * * @return UserDetailsService */ @Bean UserDetailsService userDetailsService() { return username -> User.withUsername("felord") .password("123456") .authorities("ROLE_ADMIN", "ROLE_USER") .build(); } }
/** * 测试OAuth2的控制器 * * @author felord.cn */ @RestController public class FooController { /** * 获取当前的OAuth2 Client对象实例{@link OAuth2AuthorizedClient} * 和当前认证对象实例{@link Authentication} * * @param giteeOauth2client the gitee Oauth2 client * @return the map */ @GetMapping("/foo/hello") public Map<String, Object> foo(@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient giteeOauth2client) { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Map<String, Object> map = new HashMap<>(2); map.put("giteeOauth2client", giteeOauth2client); map.put("authentication", authentication); return map; } /** * 默认登录成功跳转页为 / 防止404状态 * * @return the map */ @GetMapping("/") public Map<String, String> index() { return Collections.singletonMap("msg", "oauth2Login success!"); } }
授权成功后返回结果,也可以在你gitee的第三方应用中看到,已授权的应用
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。