修改登录接口

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
@ -59,7 +63,7 @@ public class JwtLoginFilter extends AbstractAuthenticationProcessingFilter {
@Override
public Authentication attemptAuthentication(HttpServletRequest req, HttpServletResponse res) throws UnsupportedEncodingException {
req.setCharacterEncoding("UTF-8");
res.setHeader("Access-Control-Allow-Origin","*");
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Credentials", "false");
AccountCredentials creds = new AccountCredentials();
//获取表单数据
@ -67,26 +71,22 @@ public class JwtLoginFilter extends AbstractAuthenticationProcessingFilter {
String password = req.getParameter("password");
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");
}
if(rememberMe == null||rememberMe.isEmpty()){
if (rememberMe == null || rememberMe.isEmpty()) {
rememberMe = "false";
}
creds.setUserName(userName.trim());
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,9 +95,8 @@ public class JwtLoginFilter extends AbstractAuthenticationProcessingFilter {
* @throws ServletException
*/
@Override
protected void successfulAuthentication(
HttpServletRequest req, HttpServletResponse response, FilterChain chain, Authentication auth) {
if(sysUserService==null){
protected void successfulAuthentication(HttpServletRequest req, HttpServletResponse response, FilterChain chain, Authentication auth) {
if (sysUserService == null) {
ServletContext servletContext = req.getServletContext();
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
sysUserService = webApplicationContext.getBean(SysUserService.class);
@ -110,33 +109,36 @@ 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);
map.put("city", cityByProCode);
List<Map<String, String>> countyByProCode = sysUserService.findCountyByProCode(userByUserName.getCountyCode());
map.put("county",countyByProCode);
TokenAuthenticationService.addAuthentication(response, auth.getName(),auth.getAuthorities(),userByUserName,map);
}else if(userByUserName.getCountyCode().substring(4).equals("00") && !userByUserName.getCountyCode().substring(2).equals("0000")) {
map.put("county", countyByProCode);
TokenAuthenticationService.addAuthentication(response, auth.getName(), auth.getAuthorities(), userByUserName, map);
} else if (userByUserName.getCountyCode().substring(4).equals("00") && !userByUserName.getCountyCode().substring(2).equals("0000")) {
List<Map<String, String>> countyByCityCode = sysUserService.findCountyByCityCode(userByUserName.getCountyCode());
map.put("county",countyByCityCode);
TokenAuthenticationService.addAuthentication(response, auth.getName(),auth.getAuthorities(),userByUserName,map);
}else {
map.put("county",null);
TokenAuthenticationService.addAuthentication(response, auth.getName(),auth.getAuthorities(),userByUserName,map);
map.put("county", countyByCityCode);
TokenAuthenticationService.addAuthentication(response, auth.getName(), auth.getAuthorities(), userByUserName, map);
} else {
map.put("county", null);
TokenAuthenticationService.addAuthentication(response, auth.getName(), auth.getAuthorities(), userByUserName, map);
}
}
/**
* 验证失败后调用
*
* @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");