Skip to content

Commit 1320e47

Browse files
committed
Spring Security
1 parent 2f1aecb commit 1320e47

6 files changed

Lines changed: 175 additions & 10 deletions

File tree

security/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@
2525
<artifactId>spring-boot-starter-web</artifactId>
2626
</dependency>
2727

28+
<!-- Redis -->
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-data-redis</artifactId>
32+
</dependency>
33+
<!-- redis Lettuce 模式 连接池 -->
34+
<dependency>
35+
<groupId>org.apache.commons</groupId>
36+
<artifactId>commons-pool2</artifactId>
37+
</dependency>
38+
2839
<dependency>
2940
<groupId>cn.hutool</groupId>
3041
<artifactId>hutool-all</artifactId>

security/src/main/java/org/ylc/note/security/controller/SecurityController.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
import org.springframework.web.bind.annotation.GetMapping;
44
import org.springframework.web.bind.annotation.RestController;
5+
import org.ylc.note.security.entity.User;
6+
import org.ylc.note.security.util.RedisUtils;
7+
import org.ylc.note.security.util.TokenUtil;
8+
9+
import java.util.Arrays;
10+
import java.util.List;
511

612
/**
713
* 代码千万行,注释第一行,
@@ -14,7 +20,33 @@
1420
@RestController
1521
public class SecurityController {
1622

23+
private final RedisUtils redisUtils;
24+
25+
public SecurityController(RedisUtils redisUtils) {
26+
this.redisUtils = redisUtils;
27+
}
28+
29+
/**
30+
* 模拟登陆接口
31+
*/
32+
@GetMapping("/login")
33+
public String login(String username, String password) {
34+
// 模拟登陆,实际从数据库查询
35+
User user = new User(0L, username, password);
36+
String token = TokenUtil.generateToken(user);
37+
redisUtils.set(user.getId().toString(), token, 1000 * 60 * 30);
38+
39+
// 模拟权限
40+
List<String> permissions = Arrays.asList(
41+
"/test", "/getUser"
42+
);
43+
redisUtils.strListPushAll(user.getId().toString(), permissions, 1000 * 60 * 30);
44+
return token;
45+
}
46+
47+
1748
@GetMapping("/test")
49+
1850
public String test() {
1951

2052
return "success";

security/src/main/java/org/ylc/note/security/entity/User.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@ public class User {
2222
private String username;
2323

2424
private String password;
25+
26+
public User(Long id, String username, String password) {
27+
this.id = id;
28+
this.username = username;
29+
this.password = password;
30+
}
2531
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package org.ylc.note.security.util;
2+
3+
import org.springframework.data.redis.core.StringRedisTemplate;
4+
import org.springframework.stereotype.Component;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import java.util.concurrent.TimeUnit;
9+
10+
/**
11+
* 代码全万行,注释第一行
12+
* 注释不规范,同事泪两行
13+
*
14+
* @author YuLc
15+
* @version 1.0.0
16+
* @date 2020-05-08
17+
*/
18+
@Component
19+
public class RedisUtils {
20+
21+
/**
22+
* 处理String 类型
23+
*/
24+
private final StringRedisTemplate stringRedisTemplate;
25+
26+
public RedisUtils(StringRedisTemplate stringRedisTemplate) {
27+
this.stringRedisTemplate = stringRedisTemplate;
28+
}
29+
30+
/**
31+
* 指定缓存失效时间
32+
*
33+
* @param key 键
34+
* @param time 时间(毫秒)
35+
*/
36+
public void expire(String key, long time) {
37+
if (time > 0) {
38+
stringRedisTemplate.expire(key, time, TimeUnit.MILLISECONDS);
39+
}
40+
}
41+
42+
/**
43+
* 删除缓存
44+
*
45+
* @param key 可以传一个值 或多个
46+
*/
47+
public void delete(String... key) {
48+
if (key != null && key.length > 0) {
49+
if (key.length == 1) {
50+
stringRedisTemplate.delete(key[0]);
51+
} else {
52+
stringRedisTemplate.delete(Arrays.asList(key));
53+
}
54+
}
55+
}
56+
57+
/**
58+
* 普通缓存放入
59+
*
60+
* @param key 键
61+
* @param value 值
62+
*/
63+
public void set(String key, String value) {
64+
stringRedisTemplate.opsForValue().set(key, value);
65+
}
66+
67+
/**
68+
* 普通缓存放入并设置时间
69+
*
70+
* @param key 键
71+
* @param value 值
72+
* @param time 时间(毫秒) time要大于0 如果time小于等于0 将设置无限期
73+
*/
74+
public void set(String key, String value, long time) {
75+
if (time > 0) {
76+
stringRedisTemplate.opsForValue().set(key, value, time, TimeUnit.MILLISECONDS);
77+
} else {
78+
set(key, value);
79+
}
80+
}
81+
82+
/**
83+
* 将整个list存入缓存
84+
*
85+
* @param key 键
86+
* @param strList 值
87+
*/
88+
public void strListPushAll(String key, List<String> strList, long time) {
89+
stringRedisTemplate.opsForList().rightPushAll(key, strList);
90+
if (time > 0) {
91+
expire(key, time);
92+
}
93+
}
94+
95+
}

security/src/main/java/org/ylc/note/security/util/TokenUtil.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import org.ylc.note.security.entity.User;
44

5-
import java.util.Map;
5+
import java.util.UUID;
66

77
/**
88
* 代码千万行,注释第一行,
@@ -16,21 +16,19 @@
1616
*/
1717
public class TokenUtil {
1818

19-
2019
/**
2120
* 根据用户生成token
2221
*/
23-
private String generateToken(Map<String, Object> info) {
24-
25-
return null;
22+
public static String generateToken(User user) {
23+
// 实际按照需要的业务生成token,这里模拟,返回随机数
24+
return UUID.randomUUID().toString();
2625
}
2726

2827
/**
2928
* 解析Token,返回用户信息
3029
*/
31-
private User ParseToken(String token) {
32-
33-
34-
return null;
30+
public static User ParseToken(String token) {
31+
// 模拟解析token过程
32+
return new User(0L, "admin", "123456");
3533
}
3634
}
Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
11
server:
22
servlet:
3-
context-path: /demo
3+
context-path: /demo
4+
5+
spring:
6+
redis:
7+
# Redis数据库索引(默认为0)
8+
database: 0
9+
# Redis服务器地址
10+
host: 47.110.82.103
11+
# Redis服务器连接端口
12+
port: 6379
13+
# Redis服务器连接密码(默认为空)
14+
password: 2020Root!@#$
15+
# 连接超时时间(毫秒)
16+
timeout: 3000ms
17+
lettuce:
18+
pool:
19+
# 连接池最大连接数(使用负值表示没有限制)
20+
max-active: 8
21+
# 连接池最大阻塞等待时间(使用负值表示没有限制)
22+
max-wait: 3000ms
23+
# 连接池中的最大空闲连接(负数没有限制)
24+
max-idle: 8
25+
# 连接池中的最小空闲连接
26+
min-idle: 0

0 commit comments

Comments
 (0)