Skip to content

Commit

Permalink
登录接口放开,一些代码调整
Browse files Browse the repository at this point in the history
  • Loading branch information
dolyw committed Nov 26, 2019
1 parent 0d76957 commit fad9ecd
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 10 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
2. [#19 跨域sso问题](https://github.com/dolyw/ShiroJwt/issues/19)
3. [#22 如果是微服务的话,是不是每个微服务都的写一套这样的shiro?](https://github.com/dolyw/ShiroJwt/issues/22)

<img src="https://cdn.jsdelivr.net/gh/wliduo/CDN@master/feed/qq.png" height="180"></img>

有疑问请扫码加**QQ**群交流: **779168604**

#### 项目相关

* JavaDoc:[https://apidoc.gitee.com/dolyw/ShiroJwt](https://apidoc.gitee.com/dolyw/ShiroJwt)
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/wang/config/WebMvcConfig.java
Original file line number Diff line number Diff line change
@@ -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[[email protected]]
* @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);
}
}
8 changes: 5 additions & 3 deletions src/main/java/com/wang/config/redis/JedisConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/wang/config/shiro/ShiroConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/wang/util/JwtUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/com/wang/util/UserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

/**
* 获取当前登录用户工具类
* @author Wang926454
*
* @author wliduo[[email protected]]
* @date 2019/3/15 11:45
*/
@Component
Expand All @@ -25,9 +26,10 @@ public UserUtil(UserMapper userMapper) {

/**
* 获取当前登录用户
*
* @param
* @return com.wang.model.UserDto
* @author Wang926454
* @author wliduo[[email protected]]
* @date 2019/3/15 11:48
*/
public UserDto getUser() {
Expand All @@ -46,9 +48,10 @@ public UserDto getUser() {

/**
* 获取当前登录用户Id
*
* @param
* @return com.wang.model.UserDto
* @author Wang926454
* @author wliduo[[email protected]]
* @date 2019/3/15 11:48
*/
public Integer getUserId() {
Expand All @@ -57,9 +60,10 @@ public Integer getUserId() {

/**
* 获取当前登录用户Token
*
* @param
* @return com.wang.model.UserDto
* @author Wang926454
* @author wliduo[[email protected]]
* @date 2019/3/15 11:48
*/
public String getToken() {
Expand All @@ -68,9 +72,10 @@ public String getToken() {

/**
* 获取当前登录用户Account
*
* @param
* @return com.wang.model.UserDto
* @author Wang926454
* @author wliduo[[email protected]]
* @date 2019/3/15 11:48
*/
public String getAccount() {
Expand Down

0 comments on commit fad9ecd

Please sign in to comment.