Search in sources :

Example 21 with LoginUser

use of com.ruoyi.common.core.domain.model.LoginUser in project RuoYi-Vue by yangzongzhuan.

the class SysRoleController method edit.

/**
 * 修改保存角色
 */
@PreAuthorize("@ss.hasPermi('system:role:edit')")
@Log(title = "角色管理", businessType = BusinessType.UPDATE)
@PutMapping
public AjaxResult edit(@Validated @RequestBody SysRole role) {
    roleService.checkRoleAllowed(role);
    roleService.checkRoleDataScope(role.getRoleId());
    if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
        return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,角色名称已存在");
    } else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
        return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,角色权限已存在");
    }
    role.setUpdateBy(getUsername());
    if (roleService.updateRole(role) > 0) {
        // 更新缓存用户权限
        LoginUser loginUser = getLoginUser();
        if (StringUtils.isNotNull(loginUser.getUser()) && !loginUser.getUser().isAdmin()) {
            loginUser.setPermissions(permissionService.getMenuPermission(loginUser.getUser()));
            loginUser.setUser(userService.selectUserByUserName(loginUser.getUser().getUserName()));
            tokenService.setLoginUser(loginUser);
        }
        return AjaxResult.success();
    }
    return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,请联系管理员");
}
Also used : LoginUser(com.ruoyi.common.core.domain.model.LoginUser) Log(com.ruoyi.common.annotation.Log) PutMapping(org.springframework.web.bind.annotation.PutMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 22 with LoginUser

use of com.ruoyi.common.core.domain.model.LoginUser in project RuoYi-Vue by yangzongzhuan.

the class SysUserOnlineController method list.

@PreAuthorize("@ss.hasPermi('monitor:online:list')")
@GetMapping("/list")
public TableDataInfo list(String ipaddr, String userName) {
    Collection<String> keys = redisCache.keys(Constants.LOGIN_TOKEN_KEY + "*");
    List<SysUserOnline> userOnlineList = new ArrayList<SysUserOnline>();
    for (String key : keys) {
        LoginUser user = redisCache.getCacheObject(key);
        if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName)) {
            if (StringUtils.equals(ipaddr, user.getIpaddr()) && StringUtils.equals(userName, user.getUsername())) {
                userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, user));
            }
        } else if (StringUtils.isNotEmpty(ipaddr)) {
            if (StringUtils.equals(ipaddr, user.getIpaddr())) {
                userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, 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 : SysUserOnline(com.ruoyi.system.domain.SysUserOnline) ArrayList(java.util.ArrayList) LoginUser(com.ruoyi.common.core.domain.model.LoginUser) GetMapping(org.springframework.web.bind.annotation.GetMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 23 with LoginUser

use of com.ruoyi.common.core.domain.model.LoginUser in project RuoYi-Vue by yangzongzhuan.

the class PermissionService method hasAnyPermi.

/**
 * 验证用户是否具有以下任意一个权限
 *
 * @param permissions 以 PERMISSION_NAMES_DELIMETER 为分隔符的权限列表
 * @return 用户是否具有以下任意一个权限
 */
public boolean hasAnyPermi(String permissions) {
    if (StringUtils.isEmpty(permissions)) {
        return false;
    }
    LoginUser loginUser = SecurityUtils.getLoginUser();
    if (StringUtils.isNull(loginUser) || CollectionUtils.isEmpty(loginUser.getPermissions())) {
        return false;
    }
    Set<String> authorities = loginUser.getPermissions();
    for (String permission : permissions.split(PERMISSION_DELIMETER)) {
        if (permission != null && hasPermissions(authorities, permission)) {
            return true;
        }
    }
    return false;
}
Also used : LoginUser(com.ruoyi.common.core.domain.model.LoginUser)

Example 24 with LoginUser

use of com.ruoyi.common.core.domain.model.LoginUser in project RuoYi-Vue by yangzongzhuan.

the class SysLoginService method login.

/**
 * 登录验证
 *
 * @param username 用户名
 * @param password 密码
 * @param code 验证码
 * @param uuid 唯一标识
 * @return 结果
 */
public String login(String username, String password, String code, String uuid) {
    boolean captchaOnOff = configService.selectCaptchaOnOff();
    // 验证码开关
    if (captchaOnOff) {
        validateCaptcha(username, code, uuid);
    }
    // 用户验证
    Authentication authentication = null;
    try {
        // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
        authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
    } catch (Exception e) {
        if (e instanceof BadCredentialsException) {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
            throw new UserPasswordNotMatchException();
        } else {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
            throw new ServiceException(e.getMessage());
        }
    }
    AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
    LoginUser loginUser = (LoginUser) authentication.getPrincipal();
    recordLoginInfo(loginUser.getUserId());
    // 生成token
    return tokenService.createToken(loginUser);
}
Also used : ServiceException(com.ruoyi.common.exception.ServiceException) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) UserPasswordNotMatchException(com.ruoyi.common.exception.user.UserPasswordNotMatchException) LoginUser(com.ruoyi.common.core.domain.model.LoginUser) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) ServiceException(com.ruoyi.common.exception.ServiceException) CaptchaExpireException(com.ruoyi.common.exception.user.CaptchaExpireException) CaptchaException(com.ruoyi.common.exception.user.CaptchaException) UserPasswordNotMatchException(com.ruoyi.common.exception.user.UserPasswordNotMatchException)

Example 25 with LoginUser

use of com.ruoyi.common.core.domain.model.LoginUser in project RuoYi-Vue by yangzongzhuan.

the class JwtAuthenticationTokenFilter method doFilterInternal.

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
    LoginUser loginUser = tokenService.getLoginUser(request);
    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.ruoyi.common.core.domain.model.LoginUser) WebAuthenticationDetailsSource(org.springframework.security.web.authentication.WebAuthenticationDetailsSource)

Aggregations

LoginUser (com.ruoyi.common.core.domain.model.LoginUser)65 Log (com.ruoyi.common.annotation.Log)16 SysUser (com.ruoyi.common.core.domain.entity.SysUser)13 GetMapping (org.springframework.web.bind.annotation.GetMapping)10 AjaxResult (com.ruoyi.common.core.domain.AjaxResult)9 UserType (com.ruoyi.common.enums.UserType)8 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)8 ServiceException (com.ruoyi.common.exception.ServiceException)7 ArrayList (java.util.ArrayList)7 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)7 PutMapping (org.springframework.web.bind.annotation.PutMapping)7 UserPasswordNotMatchException (com.ruoyi.common.exception.user.UserPasswordNotMatchException)5 DataColumn (com.ruoyi.common.annotation.DataColumn)4 RoleDTO (com.ruoyi.common.core.domain.dto.RoleDTO)4 SysMenu (com.ruoyi.common.core.domain.entity.SysMenu)4 CaptchaException (com.ruoyi.common.exception.user.CaptchaException)4 CaptchaExpireException (com.ruoyi.common.exception.user.CaptchaExpireException)4 Claims (io.jsonwebtoken.Claims)4 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)4 Authentication (org.springframework.security.core.Authentication)4