修改登录接口

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