feat: uaa改造为Spring Authorization Server
This commit is contained in:
-16
@@ -1,16 +0,0 @@
|
||||
package com.central.common.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 请求的方法参数上添加该注解,则注入当前登录账号的应用id
|
||||
* 例:public void test(@LoginClient String clientId) //注入webApp
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2018/7/24 16:44
|
||||
*/
|
||||
@Target(ElementType.PARAMETER)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface LoginClient {
|
||||
}
|
||||
-21
@@ -1,21 +0,0 @@
|
||||
package com.central.common.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 请求的方法参数SysUser上添加该注解,则注入当前登录人信息
|
||||
* 例1:public void test(@LoginUser SysUser user) //只有username 和 roles
|
||||
* 例2:public void test(@LoginUser(isFull = true) SysUser user) //能获取SysUser对象的所有信息
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2018/7/24 16:44
|
||||
*/
|
||||
@Target(ElementType.PARAMETER)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface LoginUser {
|
||||
/**
|
||||
* 是否查询SysUser对象所有信息,true则通过rpc接口查询
|
||||
*/
|
||||
boolean isFull() default false;
|
||||
}
|
||||
-39
@@ -1,39 +0,0 @@
|
||||
package com.central.common.config;
|
||||
|
||||
import com.central.common.feign.UserService;
|
||||
import com.central.common.resolver.ClientArgumentResolver;
|
||||
import com.central.common.resolver.TokenArgumentResolver;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 默认SpringMVC拦截器
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/8/5
|
||||
* <p>
|
||||
* Blog: http://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
public class DefaultWebMvcConfig implements WebMvcConfigurer {
|
||||
@Lazy
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
/**
|
||||
* Token参数解析
|
||||
*
|
||||
* @param argumentResolvers 解析类
|
||||
*/
|
||||
@Override
|
||||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
|
||||
//注入用户信息
|
||||
argumentResolvers.add(new TokenArgumentResolver(userService));
|
||||
//注入应用信息
|
||||
argumentResolvers.add(new ClientArgumentResolver());
|
||||
}
|
||||
}
|
||||
+6
-2
@@ -9,6 +9,8 @@ package com.central.common.constant;
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
public interface SecurityConstants {
|
||||
String PROJECT_LICENSE= "http://zlt2000.gitee.io/";
|
||||
|
||||
/**
|
||||
* 用户信息分隔符
|
||||
*/
|
||||
@@ -64,6 +66,8 @@ public interface SecurityConstants {
|
||||
*/
|
||||
String OAUTH_TOKEN_URL = "/oauth/token";
|
||||
|
||||
String OAUTH_CHECK_TOKEN_URL = "/oauth/check_token";
|
||||
|
||||
/**
|
||||
* 默认的处理验证码的url前缀
|
||||
*/
|
||||
@@ -161,11 +165,11 @@ public interface SecurityConstants {
|
||||
/**
|
||||
* redis中应用对应的token集合的key
|
||||
*/
|
||||
String REDIS_CLIENT_ID_TO_ACCESS = "client_id_to_access:";
|
||||
String REDIS_CLIENT_ID_TO_ACCESS = "client_id_to_access";
|
||||
/**
|
||||
* redis中用户名对应的token集合的key
|
||||
*/
|
||||
String REDIS_UNAME_TO_ACCESS = "uname_to_access:";
|
||||
String REDIS_UNAME_TO_ACCESS = "uname_to_access";
|
||||
/**
|
||||
* rsa公钥
|
||||
*/
|
||||
|
||||
@@ -2,7 +2,6 @@ package com.central.common.feign;
|
||||
|
||||
import com.central.common.constant.ServiceNameConstants;
|
||||
import com.central.common.feign.fallback.UserServiceFallbackFactory;
|
||||
import com.central.common.model.LoginAppUser;
|
||||
import com.central.common.model.SysRole;
|
||||
import com.central.common.model.SysUser;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
@@ -34,7 +33,7 @@ public interface UserService {
|
||||
* @return
|
||||
*/
|
||||
@GetMapping(value = "/users-anon/login", params = "username")
|
||||
LoginAppUser findByUsername(@RequestParam("username") String username);
|
||||
SysUser findByUsername(@RequestParam("username") String username);
|
||||
|
||||
/**
|
||||
* 通过手机号查询用户、角色信息
|
||||
@@ -42,7 +41,7 @@ public interface UserService {
|
||||
* @param mobile 手机号
|
||||
*/
|
||||
@GetMapping(value = "/users-anon/mobile", params = "mobile")
|
||||
LoginAppUser findByMobile(@RequestParam("mobile") String mobile);
|
||||
SysUser findByMobile(@RequestParam("mobile") String mobile);
|
||||
|
||||
/**
|
||||
* 根据OpenId查询用户信息
|
||||
@@ -50,7 +49,7 @@ public interface UserService {
|
||||
* @param openId openId
|
||||
*/
|
||||
@GetMapping(value = "/users-anon/openId", params = "openId")
|
||||
LoginAppUser findByOpenId(@RequestParam("openId") String openId);
|
||||
SysUser findByOpenId(@RequestParam("openId") String openId);
|
||||
|
||||
|
||||
/**
|
||||
|
||||
+6
-7
@@ -1,7 +1,6 @@
|
||||
package com.central.common.feign.fallback;
|
||||
|
||||
import com.central.common.feign.UserService;
|
||||
import com.central.common.model.LoginAppUser;
|
||||
import com.central.common.model.SysRole;
|
||||
import com.central.common.model.SysUser;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
@@ -28,21 +27,21 @@ public class UserServiceFallbackFactory implements FallbackFactory<UserService>
|
||||
}
|
||||
|
||||
@Override
|
||||
public LoginAppUser findByUsername(String username) {
|
||||
public SysUser findByUsername(String username) {
|
||||
log.error("通过用户名查询用户异常:{}", username, throwable);
|
||||
return new LoginAppUser();
|
||||
return new SysUser();
|
||||
}
|
||||
|
||||
@Override
|
||||
public LoginAppUser findByMobile(String mobile) {
|
||||
public SysUser findByMobile(String mobile) {
|
||||
log.error("通过手机号查询用户异常:{}", mobile, throwable);
|
||||
return new LoginAppUser();
|
||||
return new SysUser();
|
||||
}
|
||||
|
||||
@Override
|
||||
public LoginAppUser findByOpenId(String openId) {
|
||||
public SysUser findByOpenId(String openId) {
|
||||
log.error("通过openId查询用户异常:{}", openId, throwable);
|
||||
return new LoginAppUser();
|
||||
return new SysUser();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.central.common.model;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableLogic;
|
||||
@@ -39,4 +40,6 @@ public class SysUser extends SuperEntity {
|
||||
private String oldPassword;
|
||||
@TableField(exist = false)
|
||||
private String newPassword;
|
||||
@TableField(exist = false)
|
||||
private Set<String> permissions;
|
||||
}
|
||||
|
||||
-53
@@ -1,53 +0,0 @@
|
||||
package com.central.common.resolver;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.central.common.annotation.LoginClient;
|
||||
import com.central.common.constant.SecurityConstants;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* head中的应用参数注入clientId中
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/7/10
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
/**
|
||||
* 入参筛选
|
||||
*
|
||||
* @param methodParameter 参数集合
|
||||
* @return 格式化后的参数
|
||||
*/
|
||||
@Override
|
||||
public boolean supportsParameter(MethodParameter methodParameter) {
|
||||
return methodParameter.hasParameterAnnotation(LoginClient.class) && methodParameter.getParameterType().equals(String.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param methodParameter 入参集合
|
||||
* @param modelAndViewContainer model 和 view
|
||||
* @param nativeWebRequest web相关
|
||||
* @param webDataBinderFactory 入参解析
|
||||
* @return 包装对象
|
||||
*/
|
||||
@Override
|
||||
public Object resolveArgument(MethodParameter methodParameter,
|
||||
ModelAndViewContainer modelAndViewContainer,
|
||||
NativeWebRequest nativeWebRequest,
|
||||
WebDataBinderFactory webDataBinderFactory) {
|
||||
HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
|
||||
String clientId = request.getHeader(SecurityConstants.TENANT_HEADER);
|
||||
if (StrUtil.isBlank(clientId)) {
|
||||
log.warn("resolveArgument error clientId is empty");
|
||||
}
|
||||
return clientId;
|
||||
}
|
||||
}
|
||||
-64
@@ -1,64 +0,0 @@
|
||||
package com.central.common.resolver;
|
||||
|
||||
import com.central.common.annotation.LoginUser;
|
||||
import com.central.common.constant.SecurityConstants;
|
||||
import com.central.common.feign.UserService;
|
||||
import com.central.common.model.SysUser;
|
||||
import com.central.common.utils.LoginUserUtils;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
|
||||
/**
|
||||
* Token转化SysUser
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2018/12/21
|
||||
* <p>
|
||||
* Blog: http://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Slf4j
|
||||
public class TokenArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
private UserService userService;
|
||||
|
||||
public TokenArgumentResolver(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
/**
|
||||
* 入参筛选
|
||||
*
|
||||
* @param methodParameter 参数集合
|
||||
* @return 格式化后的参数
|
||||
*/
|
||||
@Override
|
||||
public boolean supportsParameter(MethodParameter methodParameter) {
|
||||
return methodParameter.hasParameterAnnotation(LoginUser.class) && methodParameter.getParameterType().equals(SysUser.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param methodParameter 入参集合
|
||||
* @param modelAndViewContainer model 和 view
|
||||
* @param nativeWebRequest web相关
|
||||
* @param webDataBinderFactory 入参解析
|
||||
* @return 包装对象
|
||||
*/
|
||||
@Override
|
||||
public Object resolveArgument(MethodParameter methodParameter,
|
||||
ModelAndViewContainer modelAndViewContainer,
|
||||
NativeWebRequest nativeWebRequest,
|
||||
WebDataBinderFactory webDataBinderFactory) {
|
||||
LoginUser loginUser = methodParameter.getParameterAnnotation(LoginUser.class);
|
||||
boolean isFull = loginUser.isFull();
|
||||
HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
|
||||
//账号类型
|
||||
String accountType = request.getHeader(SecurityConstants.ACCOUNT_TYPE_HEADER);
|
||||
|
||||
return LoginUserUtils.getCurrentUser(request, isFull);
|
||||
}
|
||||
}
|
||||
+20
-1
@@ -6,6 +6,8 @@ import com.central.common.constant.SecurityConstants;
|
||||
import com.central.common.context.LoginUserContextHolder;
|
||||
import com.central.common.feign.UserService;
|
||||
import com.central.common.model.LoginAppUser;
|
||||
import com.central.common.model.SysRole;
|
||||
import com.central.common.model.SysUser;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
@@ -49,7 +51,10 @@ public class LoginUserUtils {
|
||||
if (StrUtil.isAllNotBlank(username, userId)) {
|
||||
if (isFull) {
|
||||
UserService userService = SpringUtil.getBean(UserService.class);
|
||||
user = userService.findByUsername(username);
|
||||
SysUser sysUser = userService.findByUsername(username);
|
||||
if (sysUser != null) {
|
||||
return getLoginAppUser(sysUser);
|
||||
}
|
||||
} else {
|
||||
Collection<GrantedAuthority> authorities = new HashSet<>();
|
||||
if (StrUtil.isNotBlank(roles)) {
|
||||
@@ -112,6 +117,20 @@ public class LoginUserUtils {
|
||||
return user;
|
||||
}
|
||||
|
||||
public static LoginAppUser getLoginAppUser(SysUser sysUser) {
|
||||
List<SysRole> sysRoles = sysUser.getRoles();
|
||||
Collection<GrantedAuthority> authorities = new HashSet<>();
|
||||
if (sysRoles != null) {
|
||||
sysRoles.forEach(role -> authorities.add(new SimpleGrantedAuthority(role.getCode())));
|
||||
}
|
||||
|
||||
return new LoginAppUser(sysUser.getId()
|
||||
, sysUser.getUsername(), sysUser.getPassword()
|
||||
, sysUser.getMobile(), sysUser.getPermissions()
|
||||
, sysUser.getEnabled(), true, true, true
|
||||
, authorities);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static LoginAppUser getLoginAppUser(Map<String, Object> userObj) {
|
||||
return new LoginAppUser(
|
||||
|
||||
+1
-2
@@ -1,3 +1,2 @@
|
||||
com.central.common.feign.fallback.UserServiceFallbackFactory
|
||||
com.central.common.lock.LockAspect
|
||||
com.central.common.utils.SpringUtil
|
||||
com.central.common.lock.LockAspect
|
||||
Reference in New Issue
Block a user