修改登录接口

This commit is contained in:
liuchengqian 2022-05-07 11:04:44 +08:00
parent 7ae8bb4cfe
commit 3a3b76afc1

View File

@ -2,7 +2,7 @@ package com.xkrs.common.account;
import com.xkrs.common.encapsulation.OutputEncapsulation;
import com.xkrs.common.encapsulation.PromptMessageEnum;
import com.xkrs.model.entity.SysUserEntity;
import com.xkrs.dao.StreetDao;
import com.xkrs.model.vo.SysUserVo;
import com.xkrs.service.SysUserService;
import com.xkrs.utils.IpUtil;
@ -35,16 +35,19 @@ import java.util.Map;
/**
* jwt登录过滤器
*
* @author tajochen
*/
public class JwtLoginFilter extends AbstractAuthenticationProcessingFilter {
private static final Logger logger = LoggerFactory.getLogger(JwtLoginFilter.class);
@Resource
private SysUserService sysUserService;
@Resource
private StreetDao streetDao;
public JwtLoginFilter(String url, AuthenticationManager authManager) {
super(new AntPathRequestMatcher(url));
setAuthenticationManager(authManager);
@ -52,6 +55,7 @@ public class JwtLoginFilter extends AbstractAuthenticationProcessingFilter {
/**
* 登录时验证
*
* @param req
* @param res
* @return
@ -77,16 +81,12 @@ public class JwtLoginFilter extends AbstractAuthenticationProcessingFilter {
creds.setPassword(password.trim());
creds.setRemember(Boolean.parseBoolean(rememberMe));
// 返回一个验证令牌
return getAuthenticationManager().authenticate(
new UsernamePasswordAuthenticationToken(
creds.getUserName(),
creds.getPassword()
)
);
return getAuthenticationManager().authenticate(new UsernamePasswordAuthenticationToken(creds.getUserName(), creds.getPassword()));
}
/**
* 验证成功后调用
*
* @param req
* @param response
* @param chain
@ -95,8 +95,7 @@ public class JwtLoginFilter extends AbstractAuthenticationProcessingFilter {
* @throws ServletException
*/
@Override
protected void successfulAuthentication(
HttpServletRequest req, HttpServletResponse response, FilterChain chain, Authentication auth) {
protected void successfulAuthentication(HttpServletRequest req, HttpServletResponse response, FilterChain chain, Authentication auth) {
if (sysUserService == null) {
ServletContext servletContext = req.getServletContext();
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
@ -110,7 +109,11 @@ public class JwtLoginFilter extends AbstractAuthenticationProcessingFilter {
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
Map map = new HashMap(3);
if(userByUserName.getCountyCode().substring(2).equals("0000")){
if (9 == userByUserName.getCountyCode().length()) {
List<Map<String, String>> streetByCountyCode = streetDao.selectStreetList(userByUserName.getCountyCode());
map.put("county", streetByCountyCode);
TokenAuthenticationService.addAuthentication(response, auth.getName(), auth.getAuthorities(), userByUserName, map);
} else if (userByUserName.getCountyCode().substring(2).equals("0000")) {
List<Map<String, String>> cityByProCode = sysUserService.findCityByProCode(userByUserName.getCountyCode());
map.put("city", cityByProCode);
List<Map<String, String>> countyByProCode = sysUserService.findCountyByProCode(userByUserName.getCountyCode());
@ -128,15 +131,14 @@ public class JwtLoginFilter extends AbstractAuthenticationProcessingFilter {
/**
* 验证失败后调用
*
* @param request
* @param response
* @param failed
* @throws IOException
*/
@Override
protected void unsuccessfulAuthentication(HttpServletRequest request,
HttpServletResponse response,
AuthenticationException failed) throws IOException {
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException {
Locale locale = LocaleContextHolder.getLocale();
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Credentials", "false");