XcOauth2Util 类
package com.xuecheng.framework.utils;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* Created by mrt on 2018/5/25.
*/
public class XcOauth2Util {
public UserJwt getUserJwtFromHeader(HttpServletRequest request){
Map<String, String> jwtClaims = Oauth2Util.getJwtClaimsFromHeader(request);
if(jwtClaims == null || StringUtils.isEmpty(jwtClaims.get("id"))){
return null;
}
UserJwt userJwt = new UserJwt();
userJwt.setId(jwtClaims.get("id"));
userJwt.setName(jwtClaims.get("name"));
userJwt.setCompanyId(jwtClaims.get("companyId"));
userJwt.setUtype(jwtClaims.get("utype"));
userJwt.setUserpic(jwtClaims.get("userpic"));
return userJwt;
}
@Data
public class UserJwt{
private String id;
private String name;
private String userpic;
private String utype;
private String companyId;
}
}
Oauth2Util
package com.xuecheng.framework.utils;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.jwt.Jwt;
import org.springframework.security.jwt.JwtHelper;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* Created by mrt on 2018/5/25.
*/
public class Oauth2Util {
public static Map<String,String> getJwtClaimsFromHeader(HttpServletRequest request) {
if (request == null) {
return null;
}
//取出头信息
String authorization = request.getHeader("Authorization");
if (StringUtils.isEmpty(authorization) || authorization.indexOf("Bearer") < 0) {
return null;
}
//从Bearer 后边开始取出token
String token = authorization.substring(7);
Map<String,String> map = null;
try {
//解析jwt
Jwt decode = JwtHelper.decode(token);
//得到 jwt中的用户信息
String claims = decode.getClaims();
//将jwt转为Map
map = JSON.parseObject(claims, Map.class);
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
}
使用:
XcOauth2Util xcOauth2Util = new XcOauth2Util();
XcOauth2Util.UserJwt userJwt = xcOauth2Util.getUserJwtFromHeader(request);
String companyId = userJwt.getCompanyId();
QueryResponseResult<CourseInfo> queryResponseResult = courseService.findCourseList(companyId, page, size, courseListRequest);
return queryResponseResult;