|
|
@@ -6,6 +6,9 @@ import org.springframework.security.authentication.AuthenticationManager;
|
|
|
import org.springframework.security.authentication.BadCredentialsException;
|
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
+import org.springframework.security.core.authority.AuthorityUtils;
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
+import org.springframework.security.core.userdetails.UserDetails;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import com.ktg.common.constant.Constants;
|
|
|
import com.ktg.common.core.domain.entity.SysUser;
|
|
|
@@ -48,6 +51,9 @@ public class SysLoginService
|
|
|
@Autowired
|
|
|
private ISysConfigService configService;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private SysPermissionService permissionService;
|
|
|
+
|
|
|
/**
|
|
|
* 登录验证
|
|
|
*
|
|
|
@@ -93,6 +99,33 @@ public class SysLoginService
|
|
|
return tokenService.createToken(loginUser);
|
|
|
}
|
|
|
|
|
|
+ public String loginWithoutPassword(SysUser sysUser) {
|
|
|
+ // 用户验证
|
|
|
+ Authentication authentication;
|
|
|
+ try {
|
|
|
+ //直接不用springsecurity 认证、自己构造出数据
|
|
|
+ UserDetails userDetails = new LoginUser(sysUser.getUserId(), sysUser.getDeptId(), sysUser, permissionService.getMenuPermission(sysUser));
|
|
|
+ authentication = new UsernamePasswordAuthenticationToken(userDetails, null,
|
|
|
+ AuthorityUtils.createAuthorityList("ROLE_USER"));
|
|
|
+ SecurityContextHolder.getContext().setAuthentication(authentication);
|
|
|
+ } catch (Exception e) {
|
|
|
+ if (e instanceof BadCredentialsException) {
|
|
|
+ AsyncManager.me().execute(AsyncFactory.recordLogininfor(sysUser.getUserName(), Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
|
|
+ throw new UserPasswordNotMatchException();
|
|
|
+ } else {
|
|
|
+ AsyncManager.me().execute(AsyncFactory.recordLogininfor(sysUser.getUserName(), Constants.LOGIN_FAIL, e.getMessage()));
|
|
|
+ throw new ServiceException(e.getMessage());
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ SecurityContextHolder.clearContext();
|
|
|
+ }
|
|
|
+ AsyncManager.me().execute(AsyncFactory.recordLogininfor(sysUser.getUserName(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
|
|
+ LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
|
|
+ recordLoginInfo(loginUser.getUserId());
|
|
|
+ // 生成token
|
|
|
+ return tokenService.createToken(loginUser);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 校验验证码
|
|
|
*
|