Commit 0674ecaf authored by xiaozhe.chen's avatar xiaozhe.chen

尝试登陆功能

parent 31c7b6c5
package cn.quantgroup.customer.model.user;
import cn.quantgroup.customer.entity.User;
public class AuthBean {
private String token;
private User user;
public AuthBean(String token, User user1) {
this.token = token;
user = new User();
user.setName(user1.getName());
user.setRole(user1.getRole());
user.setUsername(user1.getUsername());
user.setId(user1.getId());
}
public AuthBean() {
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
package cn.quantgroup.customer.rest;
import cn.quantgroup.customer.entity.User;
import cn.quantgroup.customer.enums.ErrorCodeEnum;
import cn.quantgroup.customer.model.user.AuthBean;
import cn.quantgroup.customer.rest.enums.phone.ModifyPhoneApplyStatus;
import cn.quantgroup.customer.rest.enums.phone.ModifyPhoneAuditStatus;
import cn.quantgroup.customer.rest.enums.phone.ModifyPhoneProcessingStatus;
......@@ -24,6 +26,7 @@ import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.security.Principal;
import java.time.LocalDate;
import static cn.quantgroup.customer.constant.Constant.*;
......@@ -43,19 +46,15 @@ public class UserRest {
}
@PostMapping(value = "/login")
public JsonResult login(@Valid @ModelAttribute LoginParam loginParam, HttpServletRequest request) {
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(loginParam.getUsername(), loginParam.getPassword());
Authentication authentication = authenticationManager.authenticate(authRequest);
SecurityContextHolder.getContext().setAuthentication(authentication);
HttpSession session = request.getSession();
session.setAttribute(SPRING_SECURITY_CONTEXT, SecurityContextHolder.getContext());
if (authentication != null && authentication.isAuthenticated()) {
AuthenticationUserDetail principal = (AuthenticationUserDetail) authentication.getPrincipal();
AuthUserVo authUserVo = AuthUserVo.parse(principal);
return JsonResult.buildSuccessResult(LOGIN_SUCCESS, authUserVo);
public JsonResult login(Principal principal, HttpServletRequest request) {
if (principal == null) {
throw new RuntimeException("账户名或密码不正确");
}
User user = (User) ((UsernamePasswordAuthenticationToken) principal).getPrincipal();
if (!user.isEnable()) {
throw new RuntimeException("账号已被禁用");
}
return JsonResult.buildErrorStateResult(LOGIN_FAIL, null);
return new JsonResult(new AuthBean(request.getSession().getId(), user));
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment