Skip to content

Commit 2a0faef

Browse files
committed
上传技术分享
0 parents  commit 2a0faef

File tree

69 files changed

+2097
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+2097
-0
lines changed

README.txt

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
参考:
2+
https://blog.csdn.net/vbirdbest/article/details/83999188
3+
https://www.jianshu.com/p/29d7eea97339
4+
5+
jwt:
6+
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJoZWxsb2tvZGluZyIsImlhdCI6MTU3NTI3MjE2M30.aTwkRcQyWH9shRIEMRuxSB4x7uHHa4rDaj08-3RjuqI
7+
8+
清除Cookie
9+
chrome://settings/siteData
10+
11+
#生成私钥命令
12+
openssl genrsa -out rsakey0.pem 1024
13+
#生成公钥命令
14+
openssl rsa -in rsakey0.pem -pubout -out rsakey0-pub.pem

SSO-jwt-HMAC/.gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.idea/
2+
*/.settings/
3+
*.idea
4+
.DS_Store
5+
**/.iml*
6+
*.iml
7+
**/.class
8+
**/.classpath
9+
**/.project
10+
*/target/
11+
target/
12+
*.ipr
13+
*.iws
14+
antx.properties
15+
output/
16+
*/test/*
17+
logs

SSO-jwt-HMAC/SSO-client-1/.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
target
2+
out
3+
.settings
4+
.classpath
5+
.project
6+
.idea
7+
*.iml
8+
*.DS_Store

SSO-jwt-HMAC/SSO-client-1/pom.xml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.hellokoding.security</groupId>
5+
<artifactId>SSO-client-1</artifactId>
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>2.1.4.RELEASE</version>
10+
</parent>
11+
12+
<properties>
13+
<java.version>1.8</java.version>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-web</artifactId>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-freemarker</artifactId>
24+
</dependency>
25+
<dependency>
26+
<groupId>redis.clients</groupId>
27+
<artifactId>jedis</artifactId>
28+
<version>2.9.0</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>io.jsonwebtoken</groupId>
32+
<artifactId>jjwt</artifactId>
33+
<version>0.9.1</version>
34+
</dependency>
35+
</dependencies>
36+
<build>
37+
<plugins>
38+
<plugin>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-maven-plugin</artifactId>
41+
</plugin>
42+
</plugins>
43+
</build>
44+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.hellokoding.sso.resource;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.boot.web.servlet.FilterRegistrationBean;
7+
import org.springframework.context.annotation.Bean;
8+
9+
import java.util.Collections;
10+
11+
/**
12+
* @author XieShaoping
13+
*/
14+
@SpringBootApplication
15+
public class Client1Application {
16+
@Value("${services.auth}")
17+
private String authService;
18+
19+
/**
20+
* 注册过滤器
21+
*/
22+
@Bean
23+
public FilterRegistrationBean<JwtFilter> jwtFilter() {
24+
final FilterRegistrationBean<JwtFilter> registrationBean = new FilterRegistrationBean<JwtFilter>();
25+
registrationBean.setFilter(new JwtFilter());
26+
registrationBean.setInitParameters(Collections.singletonMap("services.auth", authService));
27+
registrationBean.addUrlPatterns("/protected-resource", "/logout");
28+
return registrationBean;
29+
}
30+
31+
public static void main(String[] args) throws Exception {
32+
SpringApplication.run(Client1Application.class, args);
33+
}
34+
}
35+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.hellokoding.sso.resource;
2+
3+
import org.springframework.web.util.WebUtils;
4+
import javax.servlet.http.Cookie;
5+
import javax.servlet.http.HttpServletRequest;
6+
import javax.servlet.http.HttpServletResponse;
7+
8+
/**
9+
* @author XieShaoping
10+
*/
11+
public class CookieUtil {
12+
public static void create(HttpServletResponse httpServletResponse, String name, String value, Boolean secure, Integer maxAge, String domain) {
13+
Cookie cookie = new Cookie(name, value);
14+
cookie.setSecure(secure);
15+
cookie.setHttpOnly(true);
16+
cookie.setMaxAge(maxAge);
17+
cookie.setDomain(domain);
18+
cookie.setPath("/");
19+
httpServletResponse.addCookie(cookie);
20+
}
21+
22+
public static void clear(HttpServletResponse httpServletResponse, String name) {
23+
Cookie cookie = new Cookie(name, null);
24+
cookie.setPath("/");
25+
cookie.setHttpOnly(true);
26+
cookie.setMaxAge(0);
27+
cookie.setDomain("yanxiaoping.top");//可以访问该Cookie的域名。如果设置为“.google.com”,则所有以“google.com”结尾的域名都可以访问该Cookie。
28+
httpServletResponse.addCookie(cookie);
29+
}
30+
31+
public static String getValue(HttpServletRequest httpServletRequest, String name) {
32+
Cookie cookie = WebUtils.getCookie(httpServletRequest, name);
33+
return cookie != null ? cookie.getValue() : null;
34+
}
35+
}
36+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.hellokoding.sso.resource;
2+
3+
import org.springframework.web.filter.OncePerRequestFilter;
4+
5+
import javax.servlet.FilterChain;
6+
import javax.servlet.ServletException;
7+
import javax.servlet.http.HttpServletRequest;
8+
import javax.servlet.http.HttpServletResponse;
9+
import java.io.IOException;
10+
11+
/**
12+
* @author XieShaoping
13+
*/
14+
public class JwtFilter extends OncePerRequestFilter {
15+
private static final String jwtTokenCookieName = "JWT-TOKEN";
16+
private static final String signingKey = "signingKey";
17+
18+
@Override
19+
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
20+
String username = JwtUtil.parseToken(httpServletRequest, jwtTokenCookieName, signingKey);
21+
if(username != null){
22+
httpServletRequest.setAttribute("username", username);
23+
}
24+
filterChain.doFilter(httpServletRequest, httpServletResponse);
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.hellokoding.sso.resource;
2+
3+
import io.jsonwebtoken.*;
4+
import org.slf4j.LoggerFactory;
5+
6+
import javax.servlet.http.HttpServletRequest;
7+
import java.util.Date;
8+
9+
/**
10+
* @author XieShaoping
11+
*/
12+
public class JwtUtil {
13+
private static final String REDIS_SET_ACTIVE_SUBJECTS = "active-subjects";
14+
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(JwtUtil.class);
15+
16+
public static String generateToken(String signingKey, String subject) {
17+
long nowMillis = System.currentTimeMillis();
18+
Date now = new Date(nowMillis);
19+
20+
JwtBuilder builder = Jwts.builder()
21+
.setSubject(subject)
22+
.setIssuedAt(now)
23+
.signWith(SignatureAlgorithm.HS256, signingKey);
24+
25+
String token = builder.compact();
26+
27+
RedisUtil.INSTANCE.sadd(REDIS_SET_ACTIVE_SUBJECTS, subject);
28+
29+
return token;
30+
}
31+
32+
static String parseToken(HttpServletRequest httpServletRequest, String jwtTokenCookieName, String signingKey){
33+
String token = CookieUtil.getValue(httpServletRequest, jwtTokenCookieName);
34+
if(token == null) {
35+
return null;
36+
}
37+
String subject = null;
38+
try {
39+
subject = Jwts.parser()
40+
.setSigningKey(signingKey)
41+
.parseClaimsJws(token)
42+
.getBody() //获取Claims
43+
.getSubject();
44+
} catch (ExpiredJwtException e) {
45+
logger.error("过期异常");
46+
} catch (UnsupportedJwtException e) {
47+
logger.error("不支持的Jwt异常");
48+
} catch (MalformedJwtException e) {
49+
logger.error("格式错误的Jwt异常");
50+
} catch (SignatureException e) {
51+
logger.error("签名异常");
52+
} catch (IllegalArgumentException e) {
53+
logger.error("非法参数异常");
54+
}
55+
//验证判断标准
56+
if (subject!=null&&!RedisUtil.INSTANCE.sismember(REDIS_SET_ACTIVE_SUBJECTS, subject)) {
57+
return null;
58+
}
59+
60+
return subject;
61+
}
62+
63+
static void invalidateRelatedTokens(HttpServletRequest httpServletRequest) {
64+
RedisUtil.INSTANCE.srem(REDIS_SET_ACTIVE_SUBJECTS, (String) httpServletRequest.getAttribute("username"));
65+
}
66+
}
67+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.hellokoding.sso.resource;
2+
3+
import redis.clients.jedis.Jedis;
4+
import redis.clients.jedis.JedisPool;
5+
import redis.clients.jedis.JedisPoolConfig;
6+
7+
/**
8+
* @author XieShaoping
9+
*/
10+
11+
public enum RedisUtil {
12+
/*
13+
* INSTANCE
14+
*/
15+
INSTANCE;
16+
17+
private final JedisPool pool;
18+
19+
RedisUtil() {
20+
pool = new JedisPool(new JedisPoolConfig(), "39.96.186.57");
21+
}
22+
23+
public void sadd(String key, String value) {
24+
Jedis jedis = null;
25+
try{
26+
jedis = pool.getResource();
27+
jedis.sadd(key, value);
28+
} finally {
29+
if (jedis != null) {
30+
jedis.close();
31+
}
32+
}
33+
}
34+
35+
public void srem(String key, String value) {
36+
Jedis jedis = null;
37+
try{
38+
jedis = pool.getResource();
39+
jedis.srem(key, value);
40+
} finally {
41+
if (jedis != null) {
42+
jedis.close();
43+
}
44+
}
45+
}
46+
47+
public boolean sismember(String key, String value) {
48+
Jedis jedis = null;
49+
try{
50+
jedis = pool.getResource();
51+
return jedis.sismember(key, value);
52+
} finally {
53+
if (jedis != null) {
54+
jedis.close();
55+
}
56+
}
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.hellokoding.sso.resource;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
6+
import javax.servlet.http.HttpServletRequest;
7+
import javax.servlet.http.HttpServletResponse;
8+
9+
/**
10+
* @author XieShaoping
11+
*/
12+
@Controller
13+
public class ResourceController {
14+
private static final String jwtTokenCookieName = "JWT-TOKEN";
15+
16+
@RequestMapping("/")
17+
public String home() {
18+
return "redirect:/protected-resource";
19+
}
20+
21+
@RequestMapping("/protected-resource")
22+
public String protectedResource() {
23+
return "protected-resource";
24+
}
25+
26+
@RequestMapping("/logout")
27+
public String logout(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
28+
JwtUtil.invalidateRelatedTokens(httpServletRequest);
29+
CookieUtil.clear(httpServletResponse, jwtTokenCookieName);
30+
return "redirect:/";
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
services.auth=http://native.yanxiaoping.top:8080/login
2+
server.port=8881
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Protected Resource Service</title>
5+
</head>
6+
<body>
7+
8+
<#if Request.username??>
9+
<h2><span style="color: red">本地</span>client1系统</h2>
10+
<h2>欢迎用户‘${Request.username!}’登录系统</h2>
11+
<a href="/logout">退出</a>
12+
<#else>
13+
<h2><span style="color: red">本地</span>client1系统,未登录</h2>
14+
<a href="http://native.yanxiaoping.top:8080/login?redirect=http://native.yanxiaoping.top:8881/protected-resource">请登录</a>
15+
</#if>
16+
17+
</body>
18+
</html>

SSO-jwt-HMAC/SSO-client-2/.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
target
2+
out
3+
.settings
4+
.classpath
5+
.project
6+
.idea
7+
*.iml
8+
*.DS_Store

0 commit comments

Comments
 (0)