ucard login

This commit is contained in:
ran.shang
2025-08-12 10:41:06 +08:00
parent 4526fc1cf8
commit bf59ce0a10
11 changed files with 290 additions and 35 deletions

View File

@@ -1,5 +1,6 @@
package com.newyetai.security.config;
import com.newyetai.security.ucard.UcardAuthenticationProvider;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -84,5 +85,11 @@ public class SecurityConfiguration {
SsbAuthenticationProvider ssbAuthenticationProvider(UserLoginService userLoginService) {
return new SsbAuthenticationProvider(userLoginService);
}
@Bean
@ConditionalOnBean(UserLoginService.class)
UcardAuthenticationProvider ucardAuthenticationProvider(UserLoginService userLoginService) {
return new UcardAuthenticationProvider(userLoginService);
}
}

View File

@@ -1,5 +1,7 @@
package com.newyetai.security.config;
import com.newyetai.security.ucard.UcardAuthenticationFilter;
import com.newyetai.security.ucard.UcardAuthenticationProvider;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
@@ -43,7 +45,7 @@ import lombok.extern.slf4j.Slf4j;
/**
* security配置
*
*
* @author on 2019-11-05
*/
@Slf4j
@@ -79,6 +81,9 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
@Autowired
private SsbAuthenticationProvider ssbAuthenticationProvider;
@Autowired(required = false)
private UcardAuthenticationProvider ucardAuthenticationProvider;
@Autowired
private SwaggerConfig swaggerConfig;
@@ -92,9 +97,10 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// 添加cas认证提供者
auth.authenticationProvider(upasswordAuthenticationProvider)
.authenticationProvider(mobileAuthenticationProvider)
.authenticationProvider(tokenAuthenticationProvider)
.authenticationProvider(ssbAuthenticationProvider)
.authenticationProvider(mobileAuthenticationProvider)
.authenticationProvider(tokenAuthenticationProvider)
.authenticationProvider(ssbAuthenticationProvider)
.authenticationProvider(ucardAuthenticationProvider);
;
}
@@ -146,6 +152,8 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
http.addFilterAfter(mobileAuthenticationFilter(), UpasswordAuthenticationFilter.class);
http.addFilterAfter(tokenAuthenticationFilter(), UpasswordAuthenticationFilter.class);
http.addFilterAfter(ssbAuthenticationFilter(), UpasswordAuthenticationFilter.class);
http.addFilterAfter(ucardAuthenticationFilter(), UpasswordAuthenticationFilter.class);
// 禁用CSRF
http.csrf().disable();
@@ -171,14 +179,14 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
}
}
private JwtAuthenticationTokenFilter createJwtAuthenticationTokenFilter() {
return new JwtAuthenticationTokenFilter(securityProperties);
}
/**
* 拦截器
*
*
* @return
* @throws Exception
* @author sr on 2019-11-28
@@ -196,11 +204,11 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
/**
* 手机号登录请求过滤器
*
*
* @param authenticationManager
* @return
* @author sr on 2020-06-04
* @throws Exception
* @author sr on 2020-06-04
*/
private MobileAuthenticationFilter mobileAuthenticationFilter() throws Exception {
MobileAuthenticationFilter filter = new MobileAuthenticationFilter();
@@ -216,9 +224,10 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
/**
* 账号密码
*
* @return
* @throws Exception
* @author sr on 2021-06-22
* @author sr on 2021-06-22
*/
private UpasswordAuthenticationFilter upasswordAuthenticationFilter() throws Exception {
UpasswordAuthenticationFilter filter = new UpasswordAuthenticationFilter();
@@ -228,9 +237,10 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
/**
* 临时token
*
* @return
* @throws Exception
* @author sr on 2021-06-22
* @author sr on 2021-06-22
*/
private TokenAuthenticationFilter tokenAuthenticationFilter() throws Exception {
TokenAuthenticationFilter filter = new TokenAuthenticationFilter();
@@ -244,4 +254,17 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
return filter;
}
/**
* 身份证登录
* @return
* @throws Exception
* @author sr on 2024-09-23
*/
private UcardAuthenticationFilter ucardAuthenticationFilter() throws Exception {
UcardAuthenticationFilter filter = new UcardAuthenticationFilter();
addHandler(filter);
return filter;
}
}

View File

@@ -0,0 +1,60 @@
package com.newyetai.security.ucard;
import com.newyetai.usercenter.user.login.dto.LoginUCardDTO;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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;
/**
* 账号密码认证过滤器,指定请求组装token,调起认证
*
* @author sr on 2021-06-21
*/
public class UcardAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
private boolean postOnly = true;
public static final String REQUEST_METHOD = "POST";
public static final AntPathRequestMatcher requiresAuthenticationRequestMatcher =
new AntPathRequestMatcher("/login_ucard", REQUEST_METHOD);
public static final String LOGIN_REAL_NAME_KEY = "realName";
public static final String LOGIN_CARD_NO_KEY = "cardNo";
public UcardAuthenticationFilter() {
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 realName = request.getParameter(LOGIN_REAL_NAME_KEY);
String cardNo = request.getParameter(LOGIN_CARD_NO_KEY);
realName = realName == null ? "" : realName.trim();
cardNo = cardNo == null ? "" : cardNo.trim();
AbstractAuthenticationToken authRequest = new UcardAuthenticationToken(
LoginUCardDTO.builder().realName(realName).cardNo(cardNo).build(), cardNo);
// 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));
}
}

View File

@@ -0,0 +1,64 @@
package com.newyetai.security.ucard;
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.LoginUCardDTO;
import com.newyetai.usercenter.user.login.dto.UserDetailDTO;
import com.newyetai.usercenter.user.login.service.UserLoginService;
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;
/**
* 账号密码认证provider,比对信息是否匹配
*
* @author sr on 2021-06-21
*/
public class UcardAuthenticationProvider implements AuthenticationProvider {
private UserLoginService userLoginService;
public UcardAuthenticationProvider(UserLoginService userLoginService) {
this.userLoginService = userLoginService;
}
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
LoginUCardDTO loginUCardDTO =
(LoginUCardDTO) authentication.getPrincipal();
String errMsg = ValidateUtils.validate(LoginUCardDTO.class);
Assert.isTrue(errMsg == null, new BadLoginException(errMsg));
SecurityUserDTO loadedUser = null;
try {
UserDetailDTO userDetailDTO = this.userLoginService.loadUser(loginUCardDTO);
loadedUser = UserMapMapper.INSTANCE.dtoToDto(userDetailDTO);
} catch (BadLoginException be) {
throw be;
} catch (Exception e) {
throw new BadLoginException(e.getMessage(), e);
}
UcardAuthenticationToken result =
new UcardAuthenticationToken(loadedUser, listUserGrantedAuthorities(loadedUser));
result.setDetails(authentication.getDetails());
return result;
}
@Override
public boolean supports(Class<?> authentication) {
return (UcardAuthenticationToken.class.isAssignableFrom(authentication));
}
private Set<GrantedAuthority> listUserGrantedAuthorities(UserDetails userDetails) {
// 什么都不用做,访问资源鉴权的时会拿userDetail去缓存获取对应权限
return new HashSet<GrantedAuthority>();
}
}

View File

@@ -0,0 +1,23 @@
package com.newyetai.security.ucard;
import java.util.Collection;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
/**
* 账号密码认证信息
* @author sr on 2021-06-21
*/
public class UcardAuthenticationToken extends UsernamePasswordAuthenticationToken {
public UcardAuthenticationToken(Object principal, Object credentials) {
super(principal, credentials);
}
public UcardAuthenticationToken(Object principal, Object credentials,
Collection<? extends GrantedAuthority> authorities) {
super(principal, credentials, authorities);
}
}