修改登录接口

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
@ -59,7 +63,7 @@ public class JwtLoginFilter extends AbstractAuthenticationProcessingFilter {
@Override @Override
public Authentication attemptAuthentication(HttpServletRequest req, HttpServletResponse res) throws UnsupportedEncodingException { public Authentication attemptAuthentication(HttpServletRequest req, HttpServletResponse res) throws UnsupportedEncodingException {
req.setCharacterEncoding("UTF-8"); req.setCharacterEncoding("UTF-8");
res.setHeader("Access-Control-Allow-Origin","*"); res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Credentials", "false"); res.setHeader("Access-Control-Allow-Credentials", "false");
AccountCredentials creds = new AccountCredentials(); AccountCredentials creds = new AccountCredentials();
//获取表单数据 //获取表单数据
@ -67,26 +71,22 @@ public class JwtLoginFilter extends AbstractAuthenticationProcessingFilter {
String password = req.getParameter("password"); String password = req.getParameter("password");
String rememberMe = req.getParameter("remember"); String rememberMe = req.getParameter("remember");
//如果用户名密码为空 //如果用户名密码为空
if(userName == null||password == null|| userName.trim().isEmpty()||password.trim().isEmpty()){ if (userName == null || password == null || userName.trim().isEmpty() || password.trim().isEmpty()) {
throw new BadCredentialsException("user or password is null"); throw new BadCredentialsException("user or password is null");
} }
if(rememberMe == null||rememberMe.isEmpty()){ if (rememberMe == null || rememberMe.isEmpty()) {
rememberMe = "false"; rememberMe = "false";
} }
creds.setUserName(userName.trim()); creds.setUserName(userName.trim());
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,9 +95,8 @@ 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);
sysUserService = webApplicationContext.getBean(SysUserService.class); sysUserService = webApplicationContext.getBean(SysUserService.class);
@ -110,33 +109,36 @@ 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());
map.put("county",countyByProCode); map.put("county", countyByProCode);
TokenAuthenticationService.addAuthentication(response, auth.getName(),auth.getAuthorities(),userByUserName,map); TokenAuthenticationService.addAuthentication(response, auth.getName(), auth.getAuthorities(), userByUserName, map);
}else if(userByUserName.getCountyCode().substring(4).equals("00") && !userByUserName.getCountyCode().substring(2).equals("0000")) { } else if (userByUserName.getCountyCode().substring(4).equals("00") && !userByUserName.getCountyCode().substring(2).equals("0000")) {
List<Map<String, String>> countyByCityCode = sysUserService.findCountyByCityCode(userByUserName.getCountyCode()); List<Map<String, String>> countyByCityCode = sysUserService.findCountyByCityCode(userByUserName.getCountyCode());
map.put("county",countyByCityCode); map.put("county", countyByCityCode);
TokenAuthenticationService.addAuthentication(response, auth.getName(),auth.getAuthorities(),userByUserName,map); TokenAuthenticationService.addAuthentication(response, auth.getName(), auth.getAuthorities(), userByUserName, map);
}else { } else {
map.put("county",null); map.put("county", null);
TokenAuthenticationService.addAuthentication(response, auth.getName(),auth.getAuthorities(),userByUserName,map); TokenAuthenticationService.addAuthentication(response, auth.getName(), auth.getAuthorities(), userByUserName, map);
} }
} }
/** /**
* 验证失败后调用 * 验证失败后调用
*
* @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");