From fad9ecd9845318ce8a749fd9dce0d9c6b1f00c80 Mon Sep 17 00:00:00 2001 From: dolyw <1107224733@qq.com> Date: Tue, 26 Nov 2019 14:36:14 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95=E6=8E=A5=E5=8F=A3=E6=94=BE?= =?UTF-8?q?=E5=BC=80=EF=BC=8C=E4=B8=80=E4=BA=9B=E4=BB=A3=E7=A0=81=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++++ .../java/com/wang/config/WebMvcConfig.java | 21 +++++++++++++++++++ .../com/wang/config/redis/JedisConfig.java | 8 ++++--- .../com/wang/config/shiro/ShiroConfig.java | 2 ++ src/main/java/com/wang/util/JwtUtil.java | 3 +-- src/main/java/com/wang/util/UserUtil.java | 15 ++++++++----- 6 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/wang/config/WebMvcConfig.java diff --git a/README.md b/README.md index 6ada20a..af6062c 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,10 @@ 2. [#19 跨域sso问题](https://github.com/dolyw/ShiroJwt/issues/19) 3. [#22 如果是微服务的话,是不是每个微服务都的写一套这样的shiro?](https://github.com/dolyw/ShiroJwt/issues/22) + + +有疑问请扫码加**QQ**群交流: **779168604** + #### 项目相关 * JavaDoc:[https://apidoc.gitee.com/dolyw/ShiroJwt](https://apidoc.gitee.com/dolyw/ShiroJwt) diff --git a/src/main/java/com/wang/config/WebMvcConfig.java b/src/main/java/com/wang/config/WebMvcConfig.java new file mode 100644 index 0000000..53c9483 --- /dev/null +++ b/src/main/java/com/wang/config/WebMvcConfig.java @@ -0,0 +1,21 @@ +package com.wang.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; + +/** + * 登录接口跨域放开 + * + * @author wliduo[i@dolyw.com] + * @date 2019/11/26 14:29 + */ +@Configuration +public class WebMvcConfig extends WebMvcConfigurerAdapter { + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/user/login").allowedOrigins("*") + .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS") + .allowCredentials(false).maxAge(3600); + } +} \ No newline at end of file diff --git a/src/main/java/com/wang/config/redis/JedisConfig.java b/src/main/java/com/wang/config/redis/JedisConfig.java index 03a096f..bdb74be 100644 --- a/src/main/java/com/wang/config/redis/JedisConfig.java +++ b/src/main/java/com/wang/config/redis/JedisConfig.java @@ -57,9 +57,11 @@ public JedisPool redisPoolFactory() { jedisPoolConfig.setMaxWaitMillis(maxWait); jedisPoolConfig.setMaxTotal(maxActive); jedisPoolConfig.setMinIdle(minIdle); - // JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password); - String pwd = StringUtil.isBlank(password) ? null : password; - JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, pwd); + // 密码为空设置为null + if (StringUtil.isBlank(password)) { + password = null; + } + JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password); logger.info("初始化Redis连接池JedisPool成功!地址: " + host + ":" + port); return jedisPool; } catch (Exception e) { diff --git a/src/main/java/com/wang/config/shiro/ShiroConfig.java b/src/main/java/com/wang/config/shiro/ShiroConfig.java index b63d37d..bb58ad2 100644 --- a/src/main/java/com/wang/config/shiro/ShiroConfig.java +++ b/src/main/java/com/wang/config/shiro/ShiroConfig.java @@ -87,6 +87,8 @@ public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager s // filterChainDefinitionMap.put("/doc.html", "anon"); // 公开接口 // filterChainDefinitionMap.put("/api/**", "anon"); + // 登录接口放开 + filterChainDefinitionMap.put("/user/login", "anon"); // 所有请求通过我们自己的JWTFilter filterChainDefinitionMap.put("/**", "jwt"); factoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); diff --git a/src/main/java/com/wang/util/JwtUtil.java b/src/main/java/com/wang/util/JwtUtil.java index 51f9769..838032e 100644 --- a/src/main/java/com/wang/util/JwtUtil.java +++ b/src/main/java/com/wang/util/JwtUtil.java @@ -61,8 +61,7 @@ public static boolean verify(String token) { // 帐号加JWT私钥解密 String secret = getClaim(token, Constant.ACCOUNT) + Base64ConvertUtil.decode(encryptJWTKey); Algorithm algorithm = Algorithm.HMAC256(secret); - JWTVerifier verifier = JWT.require(algorithm) - .build(); + JWTVerifier verifier = JWT.require(algorithm).build(); DecodedJWT jwt = verifier.verify(token); return true; } catch (UnsupportedEncodingException e) { diff --git a/src/main/java/com/wang/util/UserUtil.java b/src/main/java/com/wang/util/UserUtil.java index 93df735..43a8602 100644 --- a/src/main/java/com/wang/util/UserUtil.java +++ b/src/main/java/com/wang/util/UserUtil.java @@ -10,7 +10,8 @@ /** * 获取当前登录用户工具类 - * @author Wang926454 + * + * @author wliduo[i@dolyw.com] * @date 2019/3/15 11:45 */ @Component @@ -25,9 +26,10 @@ public UserUtil(UserMapper userMapper) { /** * 获取当前登录用户 + * * @param * @return com.wang.model.UserDto - * @author Wang926454 + * @author wliduo[i@dolyw.com] * @date 2019/3/15 11:48 */ public UserDto getUser() { @@ -46,9 +48,10 @@ public UserDto getUser() { /** * 获取当前登录用户Id + * * @param * @return com.wang.model.UserDto - * @author Wang926454 + * @author wliduo[i@dolyw.com] * @date 2019/3/15 11:48 */ public Integer getUserId() { @@ -57,9 +60,10 @@ public Integer getUserId() { /** * 获取当前登录用户Token + * * @param * @return com.wang.model.UserDto - * @author Wang926454 + * @author wliduo[i@dolyw.com] * @date 2019/3/15 11:48 */ public String getToken() { @@ -68,9 +72,10 @@ public String getToken() { /** * 获取当前登录用户Account + * * @param * @return com.wang.model.UserDto - * @author Wang926454 + * @author wliduo[i@dolyw.com] * @date 2019/3/15 11:48 */ public String getAccount() {