Search in sources :

Example 1 with UserInfo

use of com.junliang.spring.pojo.bean.UserInfo in project spring-boot by Linda-Tan.

the class AccessFilter method run.

@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();
    if (isIgnorePath(request.getRequestURI()))
        return null;
    String token = request.getHeader(jwtTokenHeader);
    if (StringUtils.isBlank(token)) {
        log.warn("access token is empty");
        ctx.setSendZuulResponse(false);
        ctx.setResponseStatusCode(401);
        return null;
    }
    try {
        // 校验token合法性
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(RSAHelper.getBase64PublicKey(pubKeyPath)).parseClaimsJws(token);
        Claims body = claimsJws.getBody();
        UserInfo userInfo = new UserInfo();
        userInfo.setId(body.getId());
        userInfo.setName(body.getSubject());
        log.info("parser token : {}", body);
    } catch (Exception e) {
        ctx.setResponseBody(JSON.toJSONString(new BaseResponse(ResponseCode.EX_OTHER_CODE, "Token error or Token is Expired!")));
        log.error(e.getMessage());
    }
    return null;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BaseResponse(com.junliang.spring.pojo.vo.BaseResponse) Claims(io.jsonwebtoken.Claims) UserInfo(com.junliang.spring.pojo.bean.UserInfo) RequestContext(com.netflix.zuul.context.RequestContext)

Example 2 with UserInfo

use of com.junliang.spring.pojo.bean.UserInfo in project spring-boot by Linda-Tan.

the class UserService method login.

@DataSource(name = "readDataSource")
public String login(String username, String password) {
    User user = userRepository.findByName(username);
    if (user == null)
        throw new BaseException(4001, "用户名不存在");
    if (!user.getPassword().equals(password))
        throw new BaseException(4002, "用户密码错误");
    UserInfo userInfo = new UserInfo();
    BeanCopierUtils.copyProperties(user, userInfo);
    return generateToken(userInfo);
}
Also used : BaseException(com.junliang.spring.exception.BaseException) User(com.junliang.spring.pojo.entity.User) UserInfo(com.junliang.spring.pojo.bean.UserInfo) DataSource(com.junliang.spring.aop.DataSource)

Aggregations

UserInfo (com.junliang.spring.pojo.bean.UserInfo)2 DataSource (com.junliang.spring.aop.DataSource)1 BaseException (com.junliang.spring.exception.BaseException)1 User (com.junliang.spring.pojo.entity.User)1 BaseResponse (com.junliang.spring.pojo.vo.BaseResponse)1 RequestContext (com.netflix.zuul.context.RequestContext)1 Claims (io.jsonwebtoken.Claims)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1