package com.lxzn.auth.controller;
|
|
import com.lxzn.api.auth.AuthControllerApi;
|
import com.lxzn.auth.RedisUtil;
|
import com.lxzn.auth.security.AuthService;
|
import com.lxzn.framework.domain.auth.ext.AuthToken;
|
import com.lxzn.framework.domain.auth.request.LoginRequest;
|
import com.lxzn.framework.domain.auth.response.JwtResult;
|
import com.lxzn.framework.domain.auth.response.LoginResult;
|
import com.lxzn.framework.domain.ucenter.response.UcenterCode;
|
import com.lxzn.framework.exception.ExceptionCast;
|
import com.lxzn.framework.model.response.CommonCode;
|
import com.lxzn.framework.model.response.ResponseResult;
|
import com.lxzn.framework.utils.CookieUtil;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.context.request.RequestContextHolder;
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import java.util.Map;
|
|
/**
|
* @author Administrator
|
* @version 1.0
|
**/
|
@RestController
|
@RequestMapping("/auth")
|
public class AuthController implements AuthControllerApi {
|
|
@Value("${auth.clientId}")
|
private String clientId;
|
@Value("${auth.clientSecret}")
|
private String clientSecret;
|
@Value("${auth.cookieDomain}")
|
private String cookieDomain;
|
@Value("${auth.cookieMaxAge}")
|
private int cookieMaxAge;
|
|
@Autowired
|
private AuthService authService;
|
@Autowired
|
private RedisUtil redisUtil;
|
@Value("${auth.cookieName}")
|
private String cookieName;
|
|
@Override
|
@PostMapping("/user/login")
|
public LoginResult login(LoginRequest loginRequest) {
|
if(loginRequest == null || StringUtils.isEmpty(loginRequest.getUsername())){
|
ExceptionCast.cast(UcenterCode.UCENTER_USERNAME_NONE);
|
}
|
if(loginRequest == null || StringUtils.isEmpty(loginRequest.getPassword())){
|
ExceptionCast.cast(UcenterCode.UCENTER_PASSWORD_NONE);
|
}
|
//账号
|
String username = loginRequest.getUsername();
|
//密码
|
String password = loginRequest.getPassword();
|
|
//申请令牌
|
LoginResult loginResult = authService.login(username,password,clientId,clientSecret);
|
|
//用户身份令牌
|
String access_token = loginResult.getToken();
|
//将令牌存储到cookie
|
this.saveCookie(access_token);
|
|
return loginResult;
|
}
|
|
//将令牌存储到cookie
|
private void saveCookie(String token){
|
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
|
//HttpServletResponse response,String domain,String path, String name, String value, int maxAge,boolean httpOnly
|
CookieUtil.addCookie(response,cookieDomain,"/",cookieName,token,cookieMaxAge,false);
|
|
}
|
//从cookie删除token
|
private void clearCookie(String token){
|
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
|
//HttpServletResponse response,String domain,String path, String name, String value, int maxAge,boolean httpOnly
|
CookieUtil.addCookie(response,cookieDomain,"/",cookieName,token,0,false);
|
|
}
|
|
//取出cookie中的身份令牌
|
private String getTokenFormCookie(){
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
Map<String, String> map = CookieUtil.readCookie(request, cookieName);
|
if(map!=null && map.containsKey(cookieName)){
|
return map.get(cookieName);
|
}
|
return null;
|
}
|
|
@Override
|
@PostMapping("/user/logout")
|
public ResponseResult logout() {
|
//取出cookie中的用户身份令牌
|
String uid = getTokenFormCookie();
|
//删除redis中的token
|
boolean result = redisUtil.delToken(uid);
|
//清除cookie
|
this.clearCookie(uid);
|
return new ResponseResult(CommonCode.SUCCESS);
|
}
|
|
@Override
|
@GetMapping("/user/jwt")
|
public JwtResult userJwt() {
|
//取出cookie中的用户身份令牌
|
String uid = getTokenFormCookie();
|
if(uid == null){
|
return new JwtResult(CommonCode.FAIL,null);
|
}
|
|
//拿身份令牌从redis中查询jwt令牌
|
AuthToken userToken = redisUtil.getUserToken(uid);
|
if(userToken!=null){
|
//将jwt令牌返回给用户
|
String jwt_token = userToken.getJwt_token();
|
return new JwtResult(CommonCode.SUCCESS,jwt_token);
|
}
|
return null;
|
}
|
}
|