增加sso单点登录demo

This commit is contained in:
zlt
2020-02-23 21:21:15 +08:00
parent eca520687e
commit b4d8fa93b9
9 changed files with 194 additions and 0 deletions
@@ -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
* <p>
* 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);
}
}
@@ -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
* <p>
* 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);
}
}
}
@@ -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
* <p>
* 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";
}
}
@@ -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
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta content="text/html;charset=UTF-8">
<title>SSO单点登录demo</title>
</head>
<body>
<div>
登录者: <span th:text="${username}"></span>
<br/>
权限: <span th:text="${authorities}"></span>
<br/>
应用id: <span th:text="${clientId}"></span>
<br/>
token: <span th:text="${token}"></span>
</div>
</body>
</html>