瀏覽代碼

使用配置

yufeng0528 4 年之前
父節點
當前提交
e73828fd51

+ 112 - 0
spring5-auth/spring5-auth-server/src/main/java/com/yaozhitech/spring5/config/EnvConfig.java

@@ -0,0 +1,112 @@
1
+package com.yaozhitech.spring5.config;
2
+
3
+import java.util.Arrays;
4
+import java.util.Map;
5
+
6
+import org.springframework.beans.BeansException;
7
+import org.springframework.context.ApplicationContext;
8
+import org.springframework.context.ApplicationContextAware;
9
+import org.springframework.context.ApplicationListener;
10
+import org.springframework.context.EnvironmentAware;
11
+import org.springframework.context.annotation.Configuration;
12
+import org.springframework.context.event.ContextRefreshedEvent;
13
+import org.springframework.core.env.Environment;
14
+
15
+import lombok.extern.slf4j.Slf4j;
16
+
17
+/**
18
+ * 环境配置
19
+ *
20
+ * @author shouhua
21
+ */
22
+@Slf4j
23
+@Configuration
24
+public class EnvConfig implements ApplicationListener<ContextRefreshedEvent>, EnvironmentAware, ApplicationContextAware {
25
+
26
+    private static Environment environment;
27
+
28
+    private static ApplicationContext applicationContext;
29
+    private volatile static Boolean ON_LINE;
30
+
31
+    /**
32
+     * 是否线上环境
33
+     *
34
+     * @return
35
+     */
36
+    public static boolean isOnLine() {
37
+        if (ON_LINE == null) {
38
+            ON_LINE = Arrays.asList(environment.getActiveProfiles()).contains(GlobalConstants.SPRING_PROFILE_PRODUCTION);
39
+        }
40
+        return ON_LINE;
41
+    }
42
+
43
+    /**
44
+     * 获取当前环境
45
+     *
46
+     * @return
47
+     */
48
+    public static String getEnvironment() {
49
+        if (Arrays.asList(environment.getActiveProfiles()).contains(GlobalConstants.SPRING_PROFILE_PRODUCTION)) {
50
+            return GlobalConstants.SPRING_PROFILE_PRODUCTION;
51
+        } else if (Arrays.asList(environment.getActiveProfiles()).contains(GlobalConstants.SPRING_PROFILE_DEVELOPMENT)) {
52
+            return GlobalConstants.SPRING_PROFILE_DEVELOPMENT;
53
+        } else if (Arrays.asList(environment.getActiveProfiles()).contains(GlobalConstants.SPRING_PROFILE_TEST)) {
54
+            return GlobalConstants.SPRING_PROFILE_TEST;
55
+        } else {
56
+            return environment.getActiveProfiles().toString();
57
+        }
58
+    }
59
+
60
+    /**
61
+     * 获取配置
62
+     *
63
+     * @param key
64
+     * @return
65
+     */
66
+    public static String getProperty(String key) {
67
+        return environment.getProperty(key);
68
+    }
69
+
70
+    /**
71
+     * 获取配置
72
+     *
73
+     * @param key
74
+     * @param targetType
75
+     * @param <T>
76
+     * @return
77
+     */
78
+    public static <T> T getProperty(String key, Class<T> targetType) {
79
+        return environment.getProperty(key, targetType);
80
+    }
81
+
82
+    public static <T> T getBean(Class<T> requiredType) {
83
+        return EnvConfig.applicationContext.getBean(requiredType);
84
+    }
85
+
86
+    public static <T> Map<String, T> getBeansOfType(Class<T> requiredType) {
87
+        return EnvConfig.applicationContext.getBeansOfType(requiredType);
88
+    }
89
+
90
+    public static ApplicationContext getApplicationContext() {
91
+        return EnvConfig.applicationContext;
92
+    }
93
+
94
+    @Override
95
+    public void onApplicationEvent(ContextRefreshedEvent event) {
96
+        //刷新环境
97
+        setApplicationContext(event.getApplicationContext());
98
+        setEnvironment(EnvConfig.applicationContext.getEnvironment());
99
+    }
100
+
101
+    @Override
102
+    public void setEnvironment(Environment environment) {
103
+        EnvConfig.environment = environment;
104
+        String active = EnvConfig.environment.getProperty("spring.profiles.active");
105
+        log.warn("激活[{}]配置", active);
106
+    }
107
+
108
+    @Override
109
+    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
110
+        EnvConfig.applicationContext = applicationContext;
111
+    }
112
+}

File diff suppressed because it is too large
+ 35 - 0
spring5-auth/spring5-auth-server/src/main/java/com/yaozhitech/spring5/config/GlobalConstants.java


+ 6 - 1
spring5-auth/spring5-auth-server/src/main/java/com/yaozhitech/spring5/config/ShiroConfiguration.java

@@ -16,6 +16,7 @@ import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
16 16
 import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
17 17
 import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
18 18
 import org.apache.shiro.web.mgt.DefaultWebSessionStorageEvaluator;
19
+import org.springframework.beans.factory.annotation.Value;
19 20
 import org.springframework.boot.web.servlet.FilterRegistrationBean;
20 21
 import org.springframework.context.annotation.Bean;
21 22
 import org.springframework.context.annotation.Configuration;
@@ -28,6 +29,9 @@ import com.yaozhitech.spring5.service.UserService;
28 29
 
29 30
 @Configuration
30 31
 public class ShiroConfiguration {
32
+	
33
+	@Value("${password.salt}")
34
+	private String encryptSalt;
31 35
 
32 36
 	/**
33 37
 	 * 注册shiro的Filter,拦截请求
@@ -69,7 +73,7 @@ public class ShiroConfiguration {
69 73
     */
70 74
     @Bean("dbRealm")
71 75
     public Realm dbShiroRealm(UserService userService) {
72
-        DbShiroRealm myShiroRealm = new DbShiroRealm(userService);
76
+        DbShiroRealm myShiroRealm = new DbShiroRealm(userService, encryptSalt);
73 77
         return myShiroRealm;
74 78
     }
75 79
 
@@ -119,4 +123,5 @@ public class ShiroConfiguration {
119 123
     protected AnyRolesAuthorizationFilter createRolesFilter(){
120 124
         return new AnyRolesAuthorizationFilter();
121 125
     }
126
+    
122 127
 }

+ 6 - 3
spring5-auth/spring5-auth-server/src/main/java/com/yaozhitech/spring5/jwt/DbShiroRealm.java

@@ -18,15 +18,18 @@ import org.apache.shiro.util.ByteSource;
18 18
 import com.yaozhitech.spring5.dto.UserDto;
19 19
 import com.yaozhitech.spring5.service.UserService;
20 20
 
21
+
21 22
 public class DbShiroRealm extends AuthorizingRealm {
22 23
 	
23
-	private static final String encryptSalt = "F12839WhsnnEV$#23b";
24 24
 	private UserService userService;
25 25
 	
26
-	public DbShiroRealm(UserService userService) {
26
+	private String passwordSalt;
27
+	
28
+	public DbShiroRealm(UserService userService, String passwordSalt) {
27 29
 		this.userService = userService;
28 30
 		//因为数据库中的密码做了散列,所以使用shiro的散列Matcher
29 31
 		this.setCredentialsMatcher(new HashedCredentialsMatcher(Sha256Hash.ALGORITHM_NAME));
32
+		this.passwordSalt = passwordSalt;
30 33
 	}
31 34
 	
32 35
 	/**
@@ -48,7 +51,7 @@ public class DbShiroRealm extends AuthorizingRealm {
48 51
 		if(user == null)
49 52
 			throw new AuthenticationException("用户名或者密码错误");
50 53
 		
51
-		return new SimpleAuthenticationInfo(user, user.getEncryptPwd(), ByteSource.Util.bytes(encryptSalt), "dbRealm");
54
+		return new SimpleAuthenticationInfo(user, user.getEncryptPwd(), ByteSource.Util.bytes(passwordSalt), "dbRealm");
52 55
 	}
53 56
 
54 57
 

+ 70 - 64
spring5-auth/spring5-auth-server/src/main/java/com/yaozhitech/spring5/service/UserService.java

@@ -5,6 +5,7 @@ import java.util.List;
5 5
 
6 6
 import org.apache.shiro.crypto.hash.Sha256Hash;
7 7
 import org.springframework.beans.factory.annotation.Autowired;
8
+import org.springframework.beans.factory.annotation.Value;
8 9
 import org.springframework.data.redis.core.StringRedisTemplate;
9 10
 import org.springframework.stereotype.Service;
10 11
 
@@ -14,72 +15,77 @@ import com.yaozhitech.spring5.utils.JwtUtils;
14 15
 @Service
15 16
 public class UserService {
16 17
 
17
-	private static final String encryptSalt = "F12839WhsnnEV$#23b";
18
+	@Value("${password.salt}")
19
+	private String encryptSalt;
18 20
 
19 21
 	@Autowired
20 22
 	private StringRedisTemplate redisTemplate;
21
-   
22
-    /**
23
-          *  保存user登录信息,返回token
24
-     * @param userDto
25
-     */
26
-    public String generateJwtToken(String username) {
27
-    	String salt = "12345";//JwtUtils.generateSalt();
28
-    	/**
29
-    	 * @todo 将salt保存到数据库或者缓存中
30
-    	 * redisTemplate.opsForValue().set("token:"+username, salt, 3600, TimeUnit.SECONDS);
31
-    	 */   	
32
-    	return JwtUtils.sign(username, salt, 3600); //生成jwt token,设置过期时间为1小时
33
-    }
34
-    
35
-    /**
36
-     * 获取上次token生成时的salt值和登录用户信息
37
-     * @param username
38
-     * @return
39
-     */
40
-    public UserDto getJwtTokenInfo(String username) {
41
-    	String salt = "12345";
42
-    	/**
43
-    	 * @todo 从数据库或者缓存中取出jwt token生成时用的salt
44
-    	 * salt = redisTemplate.opsForValue().get("token:"+username);
45
-    	 */   	
46
-    	UserDto user = getUserInfo(username);
47
-    	user.setSalt(salt);
48
-    	return user;
49
-    }
50 23
 
51
-    /**
52
-     * 清除token信息
53
-     * @param userName 登录用户名
54
-     * @param terminal 登录终端
55
-     */
56
-    public void deleteLoginInfo(String username) {
57
-    	/**
58
-    	 * @todo 删除数据库或者缓存中保存的salt
59
-    	 * redisTemplate.delete("token:"+username);
60
-    	 */
61
-    	
62
-    }
63
-    
64
-    /**
65
-     * 获取数据库中保存的用户信息,主要是加密后的密码
66
-     * @param userName
67
-     * @return
68
-     */
69
-    public UserDto getUserInfo(String userName) {
70
-    	UserDto user = new UserDto();
71
-    	user.setUserId(1L);
72
-    	user.setUsername("admin");
73
-    	user.setEncryptPwd(new Sha256Hash("123456", encryptSalt).toHex());
74
-    	return user;
75
-    }
76
-    
77
-    /**
78
-     * 获取用户角色列表,强烈建议从缓存中获取
79
-     * @param userId
80
-     * @return
81
-     */
82
-    public List<String> getUserRoles(Long userId){
83
-    	return Arrays.asList("admin");
84
-    }
24
+	/**
25
+	 * 保存user登录信息,返回token
26
+	 * 
27
+	 * @param userDto
28
+	 */
29
+	public String generateJwtToken(String username) {
30
+		String salt = "12345";// JwtUtils.generateSalt();
31
+		/**
32
+		 * @todo 将salt保存到数据库或者缓存中 redisTemplate.opsForValue().set("token:"+username,
33
+		 *       salt, 3600, TimeUnit.SECONDS);
34
+		 */
35
+		return JwtUtils.sign(username, salt, 3600); // 生成jwt token,设置过期时间为1小时
36
+	}
37
+
38
+	/**
39
+	 * 获取上次token生成时的salt值和登录用户信息
40
+	 * 
41
+	 * @param username
42
+	 * @return
43
+	 */
44
+	public UserDto getJwtTokenInfo(String username) {
45
+		String salt = "12345";
46
+		/**
47
+		 * @todo 从数据库或者缓存中取出jwt token生成时用的salt salt =
48
+		 *       redisTemplate.opsForValue().get("token:"+username);
49
+		 */
50
+		UserDto user = getUserInfo(username);
51
+		user.setSalt(salt);
52
+		return user;
53
+	}
54
+
55
+	/**
56
+	 * 清除token信息
57
+	 * 
58
+	 * @param userName 登录用户名
59
+	 * @param terminal 登录终端
60
+	 */
61
+	public void deleteLoginInfo(String username) {
62
+		/**
63
+		 * @todo 删除数据库或者缓存中保存的salt redisTemplate.delete("token:"+username);
64
+		 */
65
+
66
+	}
67
+
68
+	/**
69
+	 * 获取数据库中保存的用户信息,主要是加密后的密码
70
+	 * 
71
+	 * @param userName
72
+	 * @return
73
+	 */
74
+	public UserDto getUserInfo(String userName) {
75
+		UserDto user = new UserDto();
76
+		user.setUserId(1L);
77
+		user.setUsername("admin");
78
+		user.setEncryptPwd(new Sha256Hash("123456", encryptSalt).toHex());
79
+		return user;
80
+	}
81
+
82
+	/**
83
+	 * 获取用户角色列表,强烈建议从缓存中获取
84
+	 * 
85
+	 * @param userId
86
+	 * @return
87
+	 */
88
+	public List<String> getUserRoles(Long userId) {
89
+		return Arrays.asList("admin");
90
+	}
85 91
 }

+ 2 - 1
spring5-auth/spring5-auth-server/src/main/resources/application.yml

@@ -12,4 +12,5 @@ spring:
12 12
   thymeleaf:
13 13
     cache: false
14 14
   
15
-            
15
+password:
16
+  salt: k12829WhsvnEV$#03b2n