Commit 0ae1e257 by chamberone

feat: 加入全局鉴权,传递用户id参数

1 parent b0096c34
package com.dituhui.pea.gateway.config; package com.dituhui.pea.gateway.config;
import java.util.Set;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
...@@ -13,6 +16,7 @@ import com.dituhui.pea.common.Result; ...@@ -13,6 +16,7 @@ import com.dituhui.pea.common.Result;
import com.dituhui.pea.common.ResultEnum; import com.dituhui.pea.common.ResultEnum;
import com.dituhui.pea.pojo.UserLoginDTO; import com.dituhui.pea.pojo.UserLoginDTO;
import com.dituhui.pea.user.IUser; import com.dituhui.pea.user.IUser;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
...@@ -21,12 +25,21 @@ import reactor.core.publisher.Mono; ...@@ -21,12 +25,21 @@ import reactor.core.publisher.Mono;
@Slf4j @Slf4j
public class AuthFilter implements GlobalFilter, Ordered { public class AuthFilter implements GlobalFilter, Ordered {
/**
* 白名单
*/
private static final Set<String> authWhiteList = Sets.newHashSet("/pea-user/login", "/pea-user/login/");
@Autowired @Autowired
IUser userService; IUser userService;
@Override @Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
String url = exchange.getRequest().getPath().toString(); String url = exchange.getRequest().getPath().toString();
// 判断是否属于白名单中
if (authWhiteList.contains(url)) {
return chain.filter(exchange);
}
// 下面的代码从Http Header的Authorization中获取token,也可以从其他header,cookie等中获取,看客户端怎么传递token // 下面的代码从Http Header的Authorization中获取token,也可以从其他header,cookie等中获取,看客户端怎么传递token
HttpHeaders headers = exchange.getRequest().getHeaders(); HttpHeaders headers = exchange.getRequest().getHeaders();
...@@ -48,15 +61,21 @@ public class AuthFilter implements GlobalFilter, Ordered { ...@@ -48,15 +61,21 @@ public class AuthFilter implements GlobalFilter, Ordered {
UserLoginDTO userDTO = null; UserLoginDTO userDTO = null;
if (StringUtils.isNotEmpty(authToken)) { if (StringUtils.isNotEmpty(authToken)) {
// 查询token对应的用户 // 查询token对应的用户
// Result<?> userResult = userService.getUserInfo(authToken); Result<?> userResult = userService.getUserInfo(authToken);
// if (ResultEnum.SUCCESS.getCode().equals(userResult.getCode())) { if (ResultEnum.SUCCESS.getCode().equals(userResult.getCode())) {
// userDTO = (UserLoginDTO) userResult.getResult(); userDTO = (UserLoginDTO) userResult.getResult();
// } }
} }
if (userDTO == null) { if (userDTO == null) {
log.info("未授权访问{} ip:{}", url, getRemoteIP(exchange)); log.info("未授权访问{} ip:{}", url, getRemoteIP(exchange));
} else { } else {
log.info("用户{}访问{}", userDTO.getAccount(), url); log.info("用户:{} id:{} 访问{}", userDTO.getAccount(), userDTO.getId(), url);
// 获取当前的请求对象信息
ServerHttpRequest.Builder builder = exchange.getRequest().mutate();
// 向header中设置新的key,存储解析好的token对应基本信息
builder.header("userId", userDTO.getId());
// 向下游传递
return chain.filter(exchange.mutate().request(builder.build()).build());
} }
return chain.filter(exchange); return chain.filter(exchange);
......
...@@ -9,6 +9,7 @@ import lombok.Data; ...@@ -9,6 +9,7 @@ import lombok.Data;
@Data @Data
public class UserLoginDTO { public class UserLoginDTO {
private String id;
private String account; private String account;
@JsonIgnore @JsonIgnore
private String password; private String password;
......
...@@ -2,6 +2,7 @@ package com.dituhui.pea.user; ...@@ -2,6 +2,7 @@ package com.dituhui.pea.user;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
...@@ -24,7 +25,7 @@ public interface IUser { ...@@ -24,7 +25,7 @@ public interface IUser {
public Result<?> userLogin(@RequestBody UserLoginParam user); public Result<?> userLogin(@RequestBody UserLoginParam user);
@RequestMapping(value = "/pea-user/userInfo", method = RequestMethod.GET) @RequestMapping(value = "/pea-user/userInfo", method = RequestMethod.GET)
public Result<?> getUserInfo(@RequestParam(name = "token") String token); public Result<?> getUserInfo(@RequestHeader(name="userId", required = true) String userId);
/** /**
* 获取当前登陆用户信息 * 获取当前登陆用户信息
......
...@@ -40,8 +40,8 @@ public class UserController implements IUser { ...@@ -40,8 +40,8 @@ public class UserController implements IUser {
} }
@Override @Override
public Result<?> getUserInfo(String token) { public Result<?> getUserInfo(String userId) {
return userService.getUserInfo(token); return userService.getUserInfo(userId);
} }
@Override @Override
......
...@@ -148,10 +148,9 @@ public class UserService { ...@@ -148,10 +148,9 @@ public class UserService {
resourceInfo.setExtra(null); resourceInfo.setExtra(null);
} }
public Result<?> getUserInfo(String token) { public Result<?> getUserInfo(String userId) {
UserEntity user = (UserEntity) redisService.get(RedisKeyGroup.authToken + ":" + token); UserEntity user = userDao.getById(userId);
UserLoginDTO userDTO = convertToUserLoginDTO(user); UserLoginDTO userDTO = convertToUserLoginDTO(user);
userDTO.setToken(token);
return Result.success(userDTO); return Result.success(userDTO);
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!