增加网关鉴权
parent
a9023ae3d0
commit
afbcb67d0d
|
@ -4,13 +4,12 @@ import com.muyu.cloud.auth.domain.request.UserLoginReq;
|
|||
import com.muyu.cloud.auth.domain.response.TokenRes;
|
||||
import com.muyu.cloud.auth.service.AuthService;
|
||||
import com.muyu.cloud.system.domain.UserInfo;
|
||||
import com.muyu.common.constant.JwtConstants;
|
||||
import com.muyu.common.constant.TokenConstants;
|
||||
import com.muyu.common.result.Result;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* @Author: DongZeLiang
|
||||
|
@ -31,13 +30,25 @@ public class AuthController {
|
|||
* 登录
|
||||
*/
|
||||
@PostMapping("/login")
|
||||
public Result login(@RequestBody @Validated UserLoginReq userLoginReq) {
|
||||
public Result<TokenRes> login(@RequestBody @Validated UserLoginReq userLoginReq) {
|
||||
UserInfo userLoginInfo = authService.login(userLoginReq.getUserName(), userLoginReq.getPassword());
|
||||
TokenRes tokenRes = authService.genToken(userLoginInfo);
|
||||
return Result.success(tokenRes);
|
||||
}
|
||||
|
||||
@GetMapping("/login/info")
|
||||
public Result<UserInfo> getUserInfo(@RequestHeader(JwtConstants.USER_KEY) String userKey) {
|
||||
return Result.success(
|
||||
authService.loginInfo(userKey)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销
|
||||
*/
|
||||
@GetMapping("/login-out")
|
||||
public Result<String> loginOut(@RequestHeader(JwtConstants.USER_KEY) String userKey){
|
||||
authService.loginOut(userKey);
|
||||
return Result.success();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,4 +24,17 @@ public interface AuthService {
|
|||
* @return token
|
||||
*/
|
||||
TokenRes genToken (UserInfo userLoginInfo);
|
||||
|
||||
/**
|
||||
* 根据token获取用户相信信息
|
||||
* @param userKey 令牌
|
||||
* @return 用户信息
|
||||
*/
|
||||
UserInfo loginInfo (String userKey);
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
* @param userKey 令牌
|
||||
*/
|
||||
void loginOut (String userKey);
|
||||
}
|
||||
|
|
|
@ -96,4 +96,26 @@ public class AuthServiceImpl implements AuthService {
|
|||
.expiresIn(TokenConstants.EXPIRATION)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据token获取用户相信信息
|
||||
*
|
||||
* @param userKey token
|
||||
*
|
||||
* @return 用户信息
|
||||
*/
|
||||
@Override
|
||||
public UserInfo loginInfo (String userKey) {
|
||||
return redisService.getCacheObject(TokenConstants.LOGIN_TOKEN_KEY + userKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
*
|
||||
* @param userKey 令牌
|
||||
*/
|
||||
@Override
|
||||
public void loginOut (String userKey) {
|
||||
redisService.deleteObject(TokenConstants.LOGIN_TOKEN_KEY + userKey);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ import java.util.concurrent.TimeUnit;
|
|||
@Component
|
||||
public class RedisService {
|
||||
|
||||
@Scheduled(cron = "0/5 * * * * *")
|
||||
@Scheduled(cron = "0/5 * * * * ?")
|
||||
public void scheduled() {
|
||||
this.setCacheObject("skip", System.currentTimeMillis());
|
||||
log.info("Redis心跳成功");
|
||||
|
|
|
@ -4,6 +4,7 @@ import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
|
|||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
/**
|
||||
* @Author: DongZeLiang
|
||||
|
@ -12,6 +13,7 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
|||
* @Version: 1.0
|
||||
*/
|
||||
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class})
|
||||
@EnableScheduling
|
||||
public class CloudGatewayApplication {
|
||||
|
||||
public static void main (String[] args) {
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
package com.muyu.filter;
|
||||
|
||||
import com.muyu.common.constant.JwtConstants;
|
||||
import com.muyu.common.constant.TokenConstants;
|
||||
import com.muyu.common.redis.service.RedisService;
|
||||
import com.muyu.common.utils.JwtUtils;
|
||||
import com.muyu.common.utils.StringUtils;
|
||||
import com.muyu.config.IgnoreWhiteConfig;
|
||||
import com.muyu.utils.GatewayUtils;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.server.RequestPath;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
/**
|
||||
* @Author: DongZeLiang
|
||||
* @date: 2024/5/24
|
||||
* @Description: 权限过滤器
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
public class AuthFilter implements GlobalFilter, Ordered {
|
||||
|
||||
private final IgnoreWhiteConfig ignoreWhiteConfig;
|
||||
|
||||
private final RedisService redisService;
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter (ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
URI uri = request.getURI();
|
||||
String path = uri.getPath();
|
||||
if (StringUtils.matches(path, ignoreWhiteConfig.getWhites())){
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
HttpHeaders requestHeaders = request.getHeaders();
|
||||
String token = requestHeaders.getFirst(TokenConstants.TOKEN);
|
||||
if (StringUtils.isEmpty(token)){
|
||||
return GatewayUtils.errorResponse(exchange, "token不合法");
|
||||
}
|
||||
Claims claims = JwtUtils.parseToken(token);
|
||||
if (claims == null){
|
||||
return GatewayUtils.errorResponse(exchange, "token不合法");
|
||||
}
|
||||
String userKey = JwtUtils.getUserKey(claims);
|
||||
if (!redisService.hasKey(TokenConstants.LOGIN_TOKEN_KEY + userKey)){
|
||||
return GatewayUtils.errorResponse(exchange, "token已过期");
|
||||
}
|
||||
ServerHttpRequest.Builder mutate = request.mutate();
|
||||
GatewayUtils.addHeader(mutate, JwtConstants.USER_KEY, userKey);
|
||||
GatewayUtils.addHeader(mutate, JwtConstants.DETAILS_USER_ID, JwtUtils.getUserId(claims));
|
||||
GatewayUtils.addHeader(mutate, JwtConstants.DETAILS_USERNAME, JwtUtils.getUserName(claims));
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the order value of this object.
|
||||
* <p>Higher values are interpreted as lower priority. As a consequence,
|
||||
* the object with the lowest value has the highest priority (somewhat
|
||||
* analogous to Servlet {@code load-on-startup} values).
|
||||
* <p>Same order values will result in arbitrary sort positions for the
|
||||
* affected objects.
|
||||
*
|
||||
* @return the order value
|
||||
*
|
||||
* @see #HIGHEST_PRECEDENCE
|
||||
* @see #LOWEST_PRECEDENCE
|
||||
*/
|
||||
@Override
|
||||
public int getOrder () {
|
||||
return 0;
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@ package com.muyu.cloud.system;
|
|||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
/**
|
||||
* @Author: DongZeLiang
|
||||
|
@ -12,6 +13,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
|
|||
*/
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients( basePackages = {"com.muyu.**"})
|
||||
@EnableScheduling
|
||||
public class CloudSystemApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
|
Loading…
Reference in New Issue