Search in sources :

Example 1 with LoginUser

use of com.hb0730.boot.admin.security.model.LoginUser in project boot-admin by hb0730.

the class LogoutSuccessHandlerImpl method onLogoutSuccess.

@SneakyThrows
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
    LOGGER.debug("logout success <<<<<");
    User loginUser = tokenService.getLoginUser(request);
    if (null != loginUser) {
        tokenService.delLoginUser(request);
        AsyncManager.me().execute(AsyncFactory.recordLoginLog(loginUser.getUsername(), StatusEnum.SUCCESS, "登出成功"));
    }
    Result<String> result = R.success("注销成功");
    response.setStatus(200);
    response.setContentType("application/json");
    response.setCharacterEncoding("utf-8");
    response.getWriter().print(JsonUtils.objectToJson(result));
}
Also used : User(com.hb0730.boot.admin.security.model.User) SneakyThrows(lombok.SneakyThrows)

Example 2 with LoginUser

use of com.hb0730.boot.admin.security.model.LoginUser in project boot-admin by hb0730.

the class RedisTokenServiceImpl method verifyAccessToken.

@Override
public void verifyAccessToken(HttpServletRequest request) {
    User loginUser = getLoginUser(request);
    if (Objects.nonNull(loginUser)) {
        Date expireTime = loginUser.getExpireTime();
        Date currentTime = DateUtils.now();
        long refreshTime = TimeUnit.MILLISECONDS.convert(super.getProperties().getRefreshTime(), super.getProperties().getTimeUnit());
        // 校验 刷新
        if (expireTime.getTime() - currentTime.getTime() <= refreshTime) {
            String accessToken = getAccessToken(request);
            String accessTokenKey = getAccessTokenKey(accessToken);
            stringRedisTemplate.opsForValue().set(accessTokenKey, loginUser.getToken(), super.getProperties().getExpireTime(), super.getProperties().getTimeUnit());
            refreshAccessToken(loginUser);
        }
    }
}
Also used : User(com.hb0730.boot.admin.security.model.User) Date(java.util.Date)

Example 3 with LoginUser

use of com.hb0730.boot.admin.security.model.LoginUser in project boot-admin by hb0730.

the class UerOnlineServiceImpl method getOnline.

/**
 * 获取在线缓存用户信息
 *
 * @return 缓存用户
 */
private List<UserOnlineDTO> getOnline() {
    Map<String, UserDetails> online = tokenService.getOnline();
    if (!CollectionUtils.isEmpty(online)) {
        List<UserOnlineDTO> lists = Lists.newArrayList();
        for (Map.Entry<String, UserDetails> detailsEntry : online.entrySet()) {
            UserOnlineDTO dto = new UserOnlineDTO();
            dto.setTokenId(detailsEntry.getKey());
            User loginUser = (User) detailsEntry.getValue();
            BeanUtil.copyProperties(loginUser, dto);
            lists.add(dto);
        }
        return lists;
    }
    return Lists.newArrayList();
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) User(com.hb0730.boot.admin.security.model.User) UserOnlineDTO(com.hb0730.boot.admin.project.monitor.online.model.dto.UserOnlineDTO) Map(java.util.Map)

Example 4 with LoginUser

use of com.hb0730.boot.admin.security.model.LoginUser in project boot-admin by hb0730.

the class LoginServiceImpl method login.

@Nullable
public LoginUser login(@NonNull String username, @NonNull String password) {
    Authentication authenticate = null;
    try {
        // see com.hb0730.boot.admin.security.service.UserDetailsServiceImpl#loadUserByUsername
        authenticate = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
    } catch (Exception e) {
        if (e.getCause() instanceof com.hb0730.boot.admin.exceptions.UsernameNotFoundException) {
            AsyncManager.me().execute(AsyncFactory.recordLoginLog(username, StatusEnum.FAIL, "用户不存在"));
            throw new LoginException(ResponseStatusEnum.USER_NAME_NOT_FONT, "用户不存在");
        } else if (e instanceof BadCredentialsException) {
            AsyncManager.me().execute(AsyncFactory.recordLoginLog(username, StatusEnum.FAIL, "用户名或者密码错误"));
            throw new LoginException(ResponseStatusEnum.USER_PASSWORD_ERROR, "用户名或者密码错误");
        } else {
            AsyncManager.me().execute(AsyncFactory.recordLoginLog(username, StatusEnum.FAIL, e.getMessage()));
            throw new LoginException(ResponseStatusEnum.USE_LOGIN_ERROR, "登录异常,请稍后尝试", e);
        }
    }
    User user = (User) authenticate.getPrincipal();
    String accessToken = tokenService.createAccessToken(user);
    LoginUser loginUser = BeanUtil.toBean(user, LoginUser.class);
    assert loginUser != null;
    loginUser.setAccessToken(accessToken);
    AsyncManager.me().execute(AsyncFactory.recordLoginLog(username, StatusEnum.SUCCESS, "登录成功"));
    return loginUser;
}
Also used : User(com.hb0730.boot.admin.security.model.User) LoginUser(com.hb0730.boot.admin.security.model.LoginUser) Authentication(org.springframework.security.core.Authentication) LoginException(com.hb0730.boot.admin.exceptions.LoginException) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) LoginUser(com.hb0730.boot.admin.security.model.LoginUser) LoginException(com.hb0730.boot.admin.exceptions.LoginException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) Nullable(org.springframework.lang.Nullable)

Example 5 with LoginUser

use of com.hb0730.boot.admin.security.model.LoginUser in project boot-admin by hb0730.

the class AuthenticationTokenFilter method doFilterInternal.

@Override
protected void doFilterInternal(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull FilterChain filterChain) throws ServletException, IOException {
    LOGGER.debug("authentication token<<<<");
    User loginUser = tokenService.getLoginUser(request);
    if (Objects.nonNull(loginUser) && Objects.isNull(SecurityUtils.getAuthentication())) {
        tokenService.verifyAccessToken(request);
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities());
        authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
    }
    filterChain.doFilter(request, response);
}
Also used : User(com.hb0730.boot.admin.security.model.User) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) WebAuthenticationDetailsSource(org.springframework.security.web.authentication.WebAuthenticationDetailsSource)

Aggregations

User (com.hb0730.boot.admin.security.model.User)5 LoginUser (com.hb0730.boot.admin.security.model.LoginUser)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 Result (com.hb0730.boot.admin.domain.result.Result)1 LoginException (com.hb0730.boot.admin.exceptions.LoginException)1 UserOnlineDTO (com.hb0730.boot.admin.project.monitor.online.model.dto.UserOnlineDTO)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SneakyThrows (lombok.SneakyThrows)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 Nullable (org.springframework.lang.Nullable)1 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1 Authentication (org.springframework.security.core.Authentication)1 UserDetails (org.springframework.security.core.userdetails.UserDetails)1 WebAuthenticationDetailsSource (org.springframework.security.web.authentication.WebAuthenticationDetailsSource)1 MvcResult (org.springframework.test.web.servlet.MvcResult)1