This commit is contained in:
ran.shang
2025-06-11 09:42:13 +08:00
commit 7e38b8d971
1935 changed files with 132836 additions and 0 deletions

View File

@@ -0,0 +1,100 @@
package com.newyetai.security.temptoken;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import com.newyetai.security.expection.BadLoginException;
import com.newyetai.usercenter.constants.SecurityConstants;
import com.newyetai.usercenter.user.login.dto.LoginPhoneToken;
import com.newyetai.usercenter.user.login.dto.LoginUserToken;
import com.newyetai.usercenter.user.login.dto.TempTokenDTO;
/**
* 临时token认证过滤器,指定请求组装token,调起认证
*
* @author sr on 2020-06-04
*/
public class TokenAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
public static final String TYPE_KEY = "type";
public static final String REQUEST_METHOD = "POST";
private static final AntPathRequestMatcher requiresAuthenticationRequestMatcher =
new AntPathRequestMatcher("/login_token_" + "{" + TYPE_KEY + "}", REQUEST_METHOD);
private boolean postOnly = true;
public TokenAuthenticationFilter() {
super(requiresAuthenticationRequestMatcher);
}
@Override
public Authentication attemptAuthentication(HttpServletRequest request,
HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
if (postOnly && !REQUEST_METHOD.equals(request.getMethod())) {
throw new AuthenticationServiceException(
"Authentication method not supported: " + request.getMethod());
}
String type = this.resolveType(request);
String tempToken = request.getParameter(SecurityConstants.LOGIN_TEMP_TOKEN_KEY);
String entrypoint = request.getParameter(SecurityConstants.LOGIN_ENTRYPOINT_KEY);
TempTokenDTO tempTokenDTO = null;
if (TempTokenDTO.TYPE_PHONE.equals(type)) {
Long userId = null;
String userIdStr = request.getParameter(SecurityConstants.LOGIN_USER_ID_KEY);
if (StringUtils.isNotBlank(userIdStr)) {
try {
userId = Long.valueOf(userIdStr);
} catch (NumberFormatException e) {
throw new BadLoginException("用户id错误:" + userIdStr);
}
}
tempTokenDTO = LoginPhoneToken.builder().tempToken(tempToken).entrypoint(entrypoint)
.userId(userId).build();
} else if (TempTokenDTO.TYPE_USER.equals(type)) {
Long orgId = null;
String orgIdStr = request.getParameter(SecurityConstants.LOGIN_ORG_ID_KEY);
try {
orgId = Long.valueOf(orgIdStr);
} catch (NumberFormatException e) {
throw new BadLoginException("机构id错误:" + orgIdStr);
}
tempTokenDTO = LoginUserToken.builder().tempToken(tempToken).orgId(orgId).build();
} else {
throw new BadLoginException("token认证类型有误");
}
AbstractAuthenticationToken authRequest = new TokenAuthenticationToken(tempTokenDTO, tempToken);
// Allow subclasses to set the "details" property
setDetails(request, authRequest);
return this.getAuthenticationManager().authenticate(authRequest);
}
protected void setDetails(HttpServletRequest request, AbstractAuthenticationToken authRequest) {
authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
}
private String resolveType(HttpServletRequest request) {
if (requiresAuthenticationRequestMatcher.matches(request)) {
return requiresAuthenticationRequestMatcher.extractUriTemplateVariables(request)
.get(TYPE_KEY);
}
return null;
}
}

View File

@@ -0,0 +1,74 @@
package com.newyetai.security.temptoken;
import java.util.HashSet;
import java.util.Set;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import com.newyetai.common.exception.util.Assert;
import com.newyetai.common.validation.util.ValidateUtils;
import com.newyetai.security.dto.SecurityUserDTO;
import com.newyetai.security.expection.BadLoginException;
import com.newyetai.security.mapstruct.UserMapMapper;
import com.newyetai.usercenter.user.login.dto.LoginPhoneToken;
import com.newyetai.usercenter.user.login.dto.LoginUserToken;
import com.newyetai.usercenter.user.login.dto.UserDetailDTO;
import com.newyetai.usercenter.user.login.service.UserLoginService;
/**
* token认证provider,比对信息是否匹配
*
* @author sr on 2020-06-04
*/
public class TokenAuthenticationProvider implements AuthenticationProvider {
private UserLoginService userLoginService;
public TokenAuthenticationProvider(UserLoginService userLoginService) {
this.userLoginService = userLoginService;
}
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
Object principal = authentication.getPrincipal();
String errMsg = ValidateUtils.validate(principal);
Assert.isTrue(errMsg == null, new BadLoginException(errMsg));
SecurityUserDTO loadedUser = null;
try {
UserDetailDTO userDetailDTO = null;
if (principal instanceof LoginPhoneToken) {
userDetailDTO = userLoginService.loadUser((LoginPhoneToken) principal);
} else if (principal instanceof LoginUserToken) {
userDetailDTO = userLoginService.loadUser((LoginUserToken) principal);
}else {
throw new BadLoginException("token认证类型有误");
}
loadedUser = UserMapMapper.INSTANCE.dtoToDto(userDetailDTO);
} catch (BadLoginException be) {
throw be;
} catch (Exception e) {
throw new BadLoginException(e.getMessage(), e);
}
TokenAuthenticationToken result =
new TokenAuthenticationToken(loadedUser, listUserGrantedAuthorities(loadedUser));
result.setDetails(authentication.getDetails());
return result;
}
@Override
public boolean supports(Class<?> authentication) {
return (TokenAuthenticationToken.class.isAssignableFrom(authentication));
}
private Set<GrantedAuthority> listUserGrantedAuthorities(UserDetails userDetails) {
// 什么都不用做,访问资源鉴权的时会拿userDetail去缓存获取对应权限
return new HashSet<GrantedAuthority>();
}
}

View File

@@ -0,0 +1,24 @@
package com.newyetai.security.temptoken;
import java.util.Collection;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
/**
* 临时token认证信息
*
* @author sr on 2020-06-04
*/
public class TokenAuthenticationToken extends UsernamePasswordAuthenticationToken {
public TokenAuthenticationToken(Object principal, Object credentials) {
super(principal, credentials);
}
public TokenAuthenticationToken(Object principal, Object credentials,
Collection<? extends GrantedAuthority> authorities) {
super(principal, credentials, authorities);
}
}

View File

@@ -0,0 +1,25 @@
package com.newyetai.security.temptoken.dto;
import java.util.List;
import com.newyetai.usercenter.user.login.dto.PhoneToken;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
@Data
@NoArgsConstructor
@SuperBuilder
@ApiModel(description = "多用户手机号token")
public class DuplMobileToken extends PhoneToken{
/** **/
private static final long serialVersionUID = -4119001378908265360L;
@ApiModelProperty("用户列表")
private List<?> users;
}

View File

@@ -0,0 +1,25 @@
package com.newyetai.security.temptoken.dto;
import java.util.List;
import com.newyetai.usercenter.user.login.dto.UserToken;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
@Data
@NoArgsConstructor
@SuperBuilder
@ApiModel(description = "多机构用户token")
public class DuplUserToken extends UserToken{
/** **/
private static final long serialVersionUID = -374663184680420441L;
@ApiModelProperty("机构列表")
private List<?> orgs;
}

View File

@@ -0,0 +1,20 @@
package com.newyetai.security.temptoken.dto;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class PhoneTokenCreateDTO implements Serializable{
private Integer userType;
private String phone;
}

View File

@@ -0,0 +1,19 @@
package com.newyetai.security.temptoken.dto;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class UserTokenCreateDTO implements Serializable{
private Integer userType;
private Long userId;
}