diff --git a/zlt-demo/pom.xml b/zlt-demo/pom.xml index c75837ed17d2a49be90505d82822f878c8b5b673..f00ad0cdfd52da7f0e7d4734750a2db68d1c98c5 100644 --- a/zlt-demo/pom.xml +++ b/zlt-demo/pom.xml @@ -18,5 +18,7 @@ rocketmq-demo seata-demo + + sso-demo \ No newline at end of file diff --git a/zlt-demo/sso-demo/README.md b/zlt-demo/sso-demo/README.md new file mode 100644 index 0000000000000000000000000000000000000000..f95b79b8486bfc60de09254715d314fac4605157 --- /dev/null +++ b/zlt-demo/sso-demo/README.md @@ -0,0 +1,21 @@ +## **详细的原理和注意事项请查看** +[单点登录详解](https://www.kancloud.cn/zlt2000/microservices-platform/1515193) + + + +## 启动以下服务 + +1. zlt-uaa:统一认证中心 +2. user-center:用户服务 +3. sc-gateway:api网关 +4. back-web:webApp应用前端页面 +5. sso-demo:单点登录demo(zlt应用) + + + +## 测试步骤 + +1. 登录webApp应用: + 通过地址 http://127.0.0.1:8066 先登录webApp应用 +2. 访问zlt应用(单点成功): + 在浏览器打开一个新的页签(共享session),通过地址 http://127.0.0.1:8080 访问zlt应用,单点登录成功显示当前登录用户名、权限、应用id信息 \ No newline at end of file diff --git a/zlt-demo/sso-demo/pom.xml b/zlt-demo/sso-demo/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..1cce726613b38348f59d6183b49f787448777494 --- /dev/null +++ b/zlt-demo/sso-demo/pom.xml @@ -0,0 +1,50 @@ + + 4.0.0 + + com.zlt + zlt-demo + 3.2.0 + + sso-demo + 单点登录demo + + + + com.zlt + zlt-common-spring-boot-starter + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.cloud + spring-cloud-starter-oauth2 + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + com.baomidou + mybatis-plus-extension + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + + diff --git a/zlt-demo/sso-demo/src/main/java/com/sso/demo/SSOApplication.java b/zlt-demo/sso-demo/src/main/java/com/sso/demo/SSOApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..c815b94659e06ddc2a4869422a082947a7dd472d --- /dev/null +++ b/zlt-demo/sso-demo/src/main/java/com/sso/demo/SSOApplication.java @@ -0,0 +1,18 @@ +package com.sso.demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @author zlt + * @date 2020/2/22 + *

+ * Blog: https://blog.csdn.net/zlt2000 + * Github: https://github.com/zlt2000 + */ +@SpringBootApplication +public class SSOApplication { + public static void main(String[] args) { + SpringApplication.run(SSOApplication.class, args); + } +} diff --git a/zlt-demo/sso-demo/src/main/java/com/sso/demo/config/SecurityConfig.java b/zlt-demo/sso-demo/src/main/java/com/sso/demo/config/SecurityConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..68299da59bb7ff6cfcadfa11c3767389abb82622 --- /dev/null +++ b/zlt-demo/sso-demo/src/main/java/com/sso/demo/config/SecurityConfig.java @@ -0,0 +1,34 @@ +package com.sso.demo.config; + +import cn.hutool.core.util.StrUtil; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +/** + * security配置 + * + * @author zlt + * @date 2020/2/22 + *

+ * Blog: https://blog.csdn.net/zlt2000 + * Github: https://github.com/zlt2000 + */ +@EnableOAuth2Sso +@Configuration +public class SecurityConfig extends WebSecurityConfigurerAdapter { + @Value("${security.oauth2.sso.login-path:}") + private String loginPath; + + @Override + public void configure(HttpSecurity http) throws Exception { + http.authorizeRequests().anyRequest().authenticated() + .and() + .csrf().disable(); + if (StrUtil.isNotEmpty(loginPath)) { + http.formLogin().loginProcessingUrl(loginPath); + } + } +} \ No newline at end of file diff --git a/zlt-demo/sso-demo/src/main/java/com/sso/demo/controller/HomeController.java b/zlt-demo/sso-demo/src/main/java/com/sso/demo/controller/HomeController.java new file mode 100644 index 0000000000000000000000000000000000000000..a6af24a0dc4f3115060cd82adb2b87739847abc3 --- /dev/null +++ b/zlt-demo/sso-demo/src/main/java/com/sso/demo/controller/HomeController.java @@ -0,0 +1,29 @@ +package com.sso.demo.controller; + +import org.springframework.security.core.Authentication; +import org.springframework.security.oauth2.provider.OAuth2Authentication; +import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.GetMapping; + +/** + * @author zlt + * @date 2020/2/22 + *

+ * Blog: https://blog.csdn.net/zlt2000 + * Github: https://github.com/zlt2000 + */ +@Controller +public class HomeController { + @GetMapping("/") + public String home(ModelMap modelMap, Authentication authentication) { + OAuth2Authentication oauth2Authentication = (OAuth2Authentication)authentication; + modelMap.put("username", oauth2Authentication.getName()); + modelMap.put("authorities", oauth2Authentication.getAuthorities()); + modelMap.put("clientId", oauth2Authentication.getOAuth2Request().getClientId()); + OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails)oauth2Authentication.getDetails(); + modelMap.put("token", details.getTokenValue()); + return "index"; + } +} diff --git a/zlt-demo/sso-demo/src/main/resources/bootstrap.yml b/zlt-demo/sso-demo/src/main/resources/bootstrap.yml new file mode 100644 index 0000000000000000000000000000000000000000..c42f0f6293a6a19408f7d7a083c6090067e314d4 --- /dev/null +++ b/zlt-demo/sso-demo/src/main/resources/bootstrap.yml @@ -0,0 +1,22 @@ +server: + port: 8080 + +spring: + application: + name: sso-demo + +zlt: + api-uaa: + url: http://127.0.0.1:9900/api-uaa/oauth + +security: + oauth2: + sso: + login-path: /singleLogin + client: + client-id: zlt + client-secret: zlt + access-token-uri: ${zlt.api-uaa.url}/token + user-authorization-uri: ${zlt.api-uaa.url}/authorize + resource: + token-info-uri: ${zlt.api-uaa.url}/check_token diff --git a/zlt-demo/sso-demo/src/main/resources/templates/index.html b/zlt-demo/sso-demo/src/main/resources/templates/index.html new file mode 100644 index 0000000000000000000000000000000000000000..a8dede04a635ead4e78e860b5642996a00753d85 --- /dev/null +++ b/zlt-demo/sso-demo/src/main/resources/templates/index.html @@ -0,0 +1,18 @@ + + + + + SSO单点登录demo + + +

+ 登录者: +
+ 权限: +
+ 应用id: +
+ token: +
+ + diff --git "a/zlt-demo/sso-demo/\345\215\225\347\202\271\347\231\273\345\275\225\346\236\266\346\236\204\345\233\276.jpg" "b/zlt-demo/sso-demo/\345\215\225\347\202\271\347\231\273\345\275\225\346\236\266\346\236\204\345\233\276.jpg" new file mode 100644 index 0000000000000000000000000000000000000000..6262cc755e58e8e217e128050c18cb08e80ee746 Binary files /dev/null and "b/zlt-demo/sso-demo/\345\215\225\347\202\271\347\231\273\345\275\225\346\236\266\346\236\204\345\233\276.jpg" differ