Search in sources :

Example 1 with LoginUser

use of com.dimple.framework.security.LoginUser in project DimpleBlog by martin-chips.

the class JwtAuthenticationTokenFilter method doFilterInternal.

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
    LoginUser loginUser = tokenService.getLoginUser(request);
    log.info("the current request URI : {}", request.getRequestURL());
    if (StringUtils.isNotNull(loginUser) && StringUtils.isNull(SecurityUtils.getAuthentication())) {
        tokenService.verifyToken(loginUser);
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities());
        authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
    }
    chain.doFilter(request, response);
}
Also used : UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) LoginUser(com.dimple.framework.security.LoginUser) WebAuthenticationDetailsSource(org.springframework.security.web.authentication.WebAuthenticationDetailsSource)

Example 2 with LoginUser

use of com.dimple.framework.security.LoginUser in project DimpleBlog by martin-chips.

the class UserOnlineController method list.

@PreAuthorize("@permissionService.hasPermission('monitor:online:list')")
@GetMapping("/list")
public TableDataInfo list(String ip, String userName) {
    Collection<String> keys = redisCacheService.keys(Constants.LOGIN_TOKEN_KEY + "*");
    List<UserOnline> userOnlineList = new ArrayList<>();
    for (String key : keys) {
        LoginUser user = redisCacheService.getCacheObject(key);
        if (StringUtils.isNotEmpty(ip) && StringUtils.isNotEmpty(userName)) {
            if (StringUtils.equals(ip, user.getIp()) && StringUtils.equals(userName, user.getUsername())) {
                userOnlineList.add(userOnlineService.selectOnlineByInfo(ip, userName, user));
            }
        } else if (StringUtils.isNotEmpty(ip)) {
            if (StringUtils.equals(ip, user.getIp())) {
                userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ip, user));
            }
        } else if (StringUtils.isNotEmpty(userName) && StringUtils.isNotNull(user.getUser())) {
            if (StringUtils.equals(userName, user.getUsername())) {
                userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user));
            }
        } else {
            userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
        }
    }
    Collections.reverse(userOnlineList);
    userOnlineList.removeAll(Collections.singleton(null));
    return getDataTable(userOnlineList);
}
Also used : UserOnline(com.dimple.project.monitor.domain.UserOnline) ArrayList(java.util.ArrayList) LoginUser(com.dimple.framework.security.LoginUser) GetMapping(org.springframework.web.bind.annotation.GetMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 3 with LoginUser

use of com.dimple.framework.security.LoginUser in project DimpleBlog by martin-chips.

the class LogAspect method handleLog.

/**
 * for log record
 *
 * @param joinPoint  join point
 * @param e          exception
 * @param jsonResult result
 * @param cost       the time of this method cost
 */
protected void handleLog(final JoinPoint joinPoint, final Exception e, Object jsonResult, long cost) {
    try {
        // get annotation
        Log controllerLog = getAnnotationLog(joinPoint);
        if (controllerLog == null) {
            return;
        }
        // get current user from servlet
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        OperateLog operateLog = new OperateLog();
        operateLog.setStatus(Constants.SUCCESS);
        // get the IP of this request
        operateLog.setIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
        // get result with JSON format
        operateLog.setJsonResult(JSON.toJSONString(jsonResult));
        operateLog.setCost(cost);
        operateLog.setUrl(ServletUtils.getRequest().getRequestURI());
        if (loginUser != null) {
            operateLog.setOperateName(loginUser.getUsername());
        }
        if (e != null) {
            operateLog.setStatus(Constants.FAILED);
            operateLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000));
        }
        // get the class name
        String className = joinPoint.getTarget().getClass().getName();
        // get method name
        String methodName = joinPoint.getSignature().getName();
        operateLog.setMethod(StringUtils.format("{}.{}()", className, methodName));
        // get request method
        operateLog.setRequestMethod(ServletUtils.getRequest().getMethod());
        // set method args
        getControllerMethodDescription(joinPoint, controllerLog, operateLog);
        // save log
        AsyncManager.me().execute(AsyncFactory.recordOperateLog(operateLog));
    } catch (Exception exception) {
        log.error("get exception in handleLog,{} ", exception.getMessage(), exception);
    }
}
Also used : OperateLog(com.dimple.project.log.domain.OperateLog) Log(com.dimple.framework.aspectj.lang.annotation.Log) LoginUser(com.dimple.framework.security.LoginUser) TokenService(com.dimple.framework.security.service.TokenService) OperateLog(com.dimple.project.log.domain.OperateLog)

Example 4 with LoginUser

use of com.dimple.framework.security.LoginUser in project DimpleBlog by martin-chips.

the class UserServiceImpl method refreshTokenClaims.

/**
 * 同步刷新Redis缓存
 *
 * @param id sys_user id
 */
private void refreshTokenClaims(Long id) {
    // 更新redis缓存
    LoginUser loginUser = SecurityUtils.getLoginUser();
    loginUser.setUser(userMapper.selectUserById(id));
    tokenService.refreshToken(loginUser);
}
Also used : LoginUser(com.dimple.framework.security.LoginUser)

Example 5 with LoginUser

use of com.dimple.framework.security.LoginUser in project DimpleBlog by martin-chips.

the class LoginController method getRouters.

/**
 * 获取路由信息
 *
 * @return 路由信息
 */
@GetMapping("getRouters")
public AjaxResult getRouters() {
    LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
    // 用户信息
    SysUser user = loginUser.getUser();
    List<Menu> menus = menuService.selectMenuTreeByUserId(user.getId());
    return AjaxResult.success(menuService.buildMenus(menus));
}
Also used : SysUser(com.dimple.project.system.domain.SysUser) Menu(com.dimple.project.system.domain.Menu) LoginUser(com.dimple.framework.security.LoginUser) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

LoginUser (com.dimple.framework.security.LoginUser)11 GetMapping (org.springframework.web.bind.annotation.GetMapping)4 SysUser (com.dimple.project.system.domain.SysUser)3 Log (com.dimple.framework.aspectj.lang.annotation.Log)2 AjaxResult (com.dimple.framework.web.domain.AjaxResult)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 CustomException (com.dimple.common.exception.CustomException)1 CaptchaException (com.dimple.common.exception.user.CaptchaException)1 CaptchaExpireException (com.dimple.common.exception.user.CaptchaExpireException)1 UserPasswordNotMatchException (com.dimple.common.exception.user.UserPasswordNotMatchException)1 TokenService (com.dimple.framework.security.service.TokenService)1 OperateLog (com.dimple.project.log.domain.OperateLog)1 UserOnline (com.dimple.project.monitor.domain.UserOnline)1 Menu (com.dimple.project.system.domain.Menu)1 ArrayList (java.util.ArrayList)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1 Authentication (org.springframework.security.core.Authentication)1 WebAuthenticationDetailsSource (org.springframework.security.web.authentication.WebAuthenticationDetailsSource)1 PutMapping (org.springframework.web.bind.annotation.PutMapping)1