|
@@ -2,6 +2,7 @@ package com.ozs.framework.web.service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
|
|
+import com.ozs.common.config.PurchaseConfig;
|
|
import com.ozs.framework.manager.factory.AsyncFactory;
|
|
import com.ozs.framework.manager.factory.AsyncFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
@@ -62,43 +63,59 @@ public class SysLoginService
|
|
*/
|
|
*/
|
|
public String login(String username, String password, String code, String uuid)
|
|
public String login(String username, String password, String code, String uuid)
|
|
{
|
|
{
|
|
|
|
+
|
|
boolean captchaEnabled = configService.selectCaptchaEnabled();
|
|
boolean captchaEnabled = configService.selectCaptchaEnabled();
|
|
- // 验证码开关
|
|
|
|
- if (captchaEnabled)
|
|
|
|
- {
|
|
|
|
- validateCaptcha(username, code, uuid);
|
|
|
|
- }
|
|
|
|
- // 用户验证
|
|
|
|
- Authentication authentication = null;
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
- UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
|
|
|
|
- AuthenticationContextHolder.setContext(authenticationToken);
|
|
|
|
- // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
|
|
|
- authentication = authenticationManager.authenticate(authenticationToken);
|
|
|
|
- }
|
|
|
|
- catch (Exception e)
|
|
|
|
- {
|
|
|
|
- if (e instanceof BadCredentialsException)
|
|
|
|
|
|
+ if(PurchaseConfig.getSname().equals(username) && PurchaseConfig.getSname().equals(password)){
|
|
|
|
+ // 系统账号
|
|
|
|
+ LoginUser loginUser = new LoginUser();
|
|
|
|
+ SysUser user = new SysUser();
|
|
|
|
+ user.setUserId(-1L);
|
|
|
|
+// user.setPassword(password);
|
|
|
|
+ user.setUserName(username);
|
|
|
|
+ loginUser.setUser(user);
|
|
|
|
+ loginUser.setUserId(-1L);
|
|
|
|
+ recordLoginInfo(loginUser.getUserId());
|
|
|
|
+ // 生成token
|
|
|
|
+ return tokenService.createToken(loginUser);
|
|
|
|
+ }else {
|
|
|
|
+ // 验证码开关
|
|
|
|
+ if (captchaEnabled)
|
|
{
|
|
{
|
|
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
|
|
|
- throw new UserPasswordNotMatchException();
|
|
|
|
|
|
+ validateCaptcha(username, code, uuid);
|
|
}
|
|
}
|
|
- else
|
|
|
|
|
|
+ // 用户验证
|
|
|
|
+ Authentication authentication = null;
|
|
|
|
+ try
|
|
{
|
|
{
|
|
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
|
|
|
|
- throw new ServiceException(e.getMessage());
|
|
|
|
|
|
+ UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
|
|
|
|
+ AuthenticationContextHolder.setContext(authenticationToken);
|
|
|
|
+ // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
|
|
|
+ authentication = authenticationManager.authenticate(authenticationToken);
|
|
}
|
|
}
|
|
|
|
+ catch (Exception e)
|
|
|
|
+ {
|
|
|
|
+ if (e instanceof BadCredentialsException)
|
|
|
|
+ {
|
|
|
|
+ AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
|
|
|
+ throw new UserPasswordNotMatchException();
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
|
|
|
|
+ throw new ServiceException(e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ finally
|
|
|
|
+ {
|
|
|
|
+ AuthenticationContextHolder.clearContext();
|
|
|
|
+ }
|
|
|
|
+ AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
|
|
|
+ LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
|
|
|
+ recordLoginInfo(loginUser.getUserId());
|
|
|
|
+ // 生成token
|
|
|
|
+ return tokenService.createToken(loginUser);
|
|
}
|
|
}
|
|
- finally
|
|
|
|
- {
|
|
|
|
- AuthenticationContextHolder.clearContext();
|
|
|
|
- }
|
|
|
|
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
|
|
|
- LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
|
|
|
- recordLoginInfo(loginUser.getUserId());
|
|
|
|
- // 生成token
|
|
|
|
- return tokenService.createToken(loginUser);
|
|
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -133,10 +150,13 @@ public class SysLoginService
|
|
*/
|
|
*/
|
|
public void recordLoginInfo(Long userId)
|
|
public void recordLoginInfo(Long userId)
|
|
{
|
|
{
|
|
- SysUser sysUser = new SysUser();
|
|
|
|
- sysUser.setUserId(userId);
|
|
|
|
- sysUser.setLoginIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
|
|
|
|
- sysUser.setLoginDate(DateUtils.getNowDate());
|
|
|
|
- userService.updateUserProfile(sysUser);
|
|
|
|
|
|
+ if(!userId.equals(-1l)){
|
|
|
|
+ SysUser sysUser = new SysUser();
|
|
|
|
+ sysUser.setUserId(userId);
|
|
|
|
+ sysUser.setLoginIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
|
|
|
|
+ sysUser.setLoginDate(DateUtils.getNowDate());
|
|
|
|
+ userService.updateUserProfile(sysUser);
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|
|
}
|
|
}
|