Search in sources :

Example 1 with TokenManager

use of com.moon.admin.service.TokenManager in project moon by gentoo111.

the class LogoutFilter method preHandle.

@Override
protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
    String loginToken = RestfulFilter.getToken(request);
    User user = UserUtil.getCurrentUser();
    if (StringUtils.isBlank(loginToken)) {
        // 非Restful方式
        boolean flag = super.preHandle(request, response);
        log.debug("{}退出成功", user.getUsername());
        SpringUtil.getBean(SysLogService.class).save(user.getId(), "退出", true, null);
        return flag;
    } else {
        TokenManager tokenManager = SpringUtil.getBean(TokenManager.class);
        boolean flag = tokenManager.deleteToken(loginToken);
        if (flag) {
            RestfulFilter.writeResponse(WebUtils.toHttp(response), HttpStatus.OK.value(), SUCCESS_INFO);
            log.debug("{}退出成功", user.getUsername());
        } else {
            RestfulFilter.writeResponse(WebUtils.toHttp(response), HttpStatus.BAD_REQUEST.value(), ERR_INFO);
        }
        SpringUtil.getBean(SysLogService.class).save(user.getId(), "token方式退出", flag, null);
        return false;
    }
}
Also used : User(com.moon.admin.domain.User) SysLogService(com.moon.admin.service.SysLogService) TokenManager(com.moon.admin.service.TokenManager)

Example 2 with TokenManager

use of com.moon.admin.service.TokenManager in project moon by gentoo111.

the class RestfulFilter method isAccessAllowed.

@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
    String loginToken = getToken(request);
    if (StringUtils.isBlank(loginToken)) {
        // 非Restful方式
        return super.isAccessAllowed(request, response, mappedValue);
    }
    TokenManager tokenManager = SpringUtil.getBean(TokenManager.class);
    UsernamePasswordToken token = tokenManager.getToken(loginToken);
    if (token != null) {
        try {
            Subject subject = getSubject(request, response);
            if (subject.getPrincipal() == null) {
                subject.login(token);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return false;
}
Also used : TokenManager(com.moon.admin.service.TokenManager) Subject(org.apache.shiro.subject.Subject) IOException(java.io.IOException) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken)

Aggregations

TokenManager (com.moon.admin.service.TokenManager)2 User (com.moon.admin.domain.User)1 SysLogService (com.moon.admin.service.SysLogService)1 IOException (java.io.IOException)1 UsernamePasswordToken (org.apache.shiro.authc.UsernamePasswordToken)1 Subject (org.apache.shiro.subject.Subject)1