Search in sources :

Example 1 with AuthUser

use of com.github.liuweijw.system.api.model.AuthUser in project fw-cloud-framework by liuweijw.

the class UserServiceImpl method findUserByMobile.

@Override
@Cacheable(key = "'user_mobile_' + #mobile")
public AuthUser findUserByMobile(String mobile) {
    User user = userRepository.findUserByMobile(mobile.trim());
    if (null == user)
        return null;
    user.setRoleList(findRoleListByUserId(user.getUserId()));
    return buildAuthUserByUser(user);
}
Also used : User(com.github.liuweijw.business.admin.domain.User) AuthUser(com.github.liuweijw.system.api.model.AuthUser) QUser(com.github.liuweijw.business.admin.domain.QUser) Cacheable(org.springframework.cache.annotation.Cacheable)

Example 2 with AuthUser

use of com.github.liuweijw.system.api.model.AuthUser in project fw-cloud-framework by liuweijw.

the class UserServiceImpl method buildAuthUserByUser.

private AuthUser buildAuthUserByUser(User user) {
    if (null == user)
        return null;
    AuthUser authUser = new AuthUser();
    authUser.setPicUrl(user.getPicUrl());
    authUser.setStatu(user.getStatu());
    authUser.setPassword(user.getPassword());
    authUser.setUserId(user.getUserId());
    authUser.setUsername(user.getUsername());
    if (null == user.getRoleList() || user.getRoleList().size() == 0)
        return authUser;
    List<AuthRole> rList = new ArrayList<AuthRole>();
    for (Role r : user.getRoleList()) {
        AuthRole aRole = new AuthRole();
        aRole.setStatu(r.getStatu());
        aRole.setRoleCode(r.getRoleCode());
        aRole.setRoleDesc(r.getRoleDesc());
        aRole.setRoleId(r.getRoleId());
        aRole.setRoleName(r.getRoleName());
        rList.add(aRole);
    }
    authUser.setRoleList(rList);
    return authUser;
}
Also used : QUserRole(com.github.liuweijw.business.admin.domain.QUserRole) AuthRole(com.github.liuweijw.system.api.model.AuthRole) Role(com.github.liuweijw.business.admin.domain.Role) QRole(com.github.liuweijw.business.admin.domain.QRole) UserRole(com.github.liuweijw.business.admin.domain.UserRole) AuthRole(com.github.liuweijw.system.api.model.AuthRole) ArrayList(java.util.ArrayList) AuthUser(com.github.liuweijw.system.api.model.AuthUser)

Example 3 with AuthUser

use of com.github.liuweijw.system.api.model.AuthUser in project fw-cloud-framework by liuweijw.

the class TokenArgumentResolver method resolveArgument.

/**
 * 1. 先读缓存 2. 不存在缓存,解析token 获取用户信息 3. 对API含有 AuthUser入参的接口生效
 */
@Override
public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) {
    HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
    String username = request.getHeader(SecurityConstant.USER_HEADER);
    String roles = request.getHeader(SecurityConstant.ROLE_HEADER);
    if (StringHelper.isBlank(username) || StringHelper.isBlank(roles)) {
        log.warn("resolveArgument error username or role is empty");
        return null;
    }
    AuthUser authUser = new AuthUser();
    authUser.setUsername(username);
    List<AuthRole> roleList = new ArrayList<AuthRole>();
    Arrays.stream(roles.split(",")).forEach(role -> {
        AuthRole authRole = new AuthRole();
        authRole.setRoleCode(role);
        roleList.add(authRole);
    });
    authUser.setRoleList(roleList);
    return authUser;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) AuthRole(com.github.liuweijw.system.api.model.AuthRole) ArrayList(java.util.ArrayList) AuthUser(com.github.liuweijw.system.api.model.AuthUser)

Example 4 with AuthUser

use of com.github.liuweijw.system.api.model.AuthUser in project fw-cloud-framework by liuweijw.

the class AjaxAuthenticationProvider method authenticate.

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    AjaxAuthenticationToken ajaxAuthenticationToken = (AjaxAuthenticationToken) authentication;
    AuthUser user = userFeignApi.findUserByMobile((String) ajaxAuthenticationToken.getPrincipal());
    if (null == user)
        throw new UsernameNotFoundException("登录账户[" + ajaxAuthenticationToken.getPrincipal() + "]不存在");
    UserDetailsImpl userDetails = buildUserDeatils(user);
    if (null == userDetails)
        throw new InternalAuthenticationServiceException("登录用户[" + ajaxAuthenticationToken.getPrincipal() + "]不存在!");
    AjaxAuthenticationToken authenticationToken = new AjaxAuthenticationToken(userDetails, userDetails.getAuthorities());
    authenticationToken.setDetails(ajaxAuthenticationToken.getDetails());
    return authenticationToken;
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDetailsImpl(com.github.liuweijw.system.auth.service.UserDetailsImpl) InternalAuthenticationServiceException(org.springframework.security.authentication.InternalAuthenticationServiceException) AuthUser(com.github.liuweijw.system.api.model.AuthUser)

Example 5 with AuthUser

use of com.github.liuweijw.system.api.model.AuthUser in project fw-cloud-framework by liuweijw.

the class UserServiceImpl method findUserInfo.

@Override
public UserBean findUserInfo(AuthUser user) {
    User dbUser = findUserByUsername(user.getUsername(), false);
    UserBean userInfo = new UserBean();
    // 过滤关键信息
    dbUser.setPassword("");
    dbUser.setCreateTime(null);
    dbUser.setUpdateTime(null);
    userInfo.setUser(dbUser);
    // 设置角色列表
    List<AuthRole> roleList = user.getRoleList();
    List<String> roleCodes = new ArrayList<>();
    roleList.stream().forEach(r -> {
        roleCodes.add(r.getRoleCode());
    });
    String[] roles = roleCodes.toArray(new String[roleCodes.size()]);
    userInfo.setRoles(roles);
    // 设置权限列表(menu.permission)
    Set<String> permissions = new HashSet<String>();
    for (String roleCode : roles) {
        permissions.addAll(permissionService.findMenuPermissions(roleCode));
    }
    userInfo.setPermissions(permissions.toArray(new String[permissions.size()]));
    return userInfo;
}
Also used : User(com.github.liuweijw.business.admin.domain.User) AuthUser(com.github.liuweijw.system.api.model.AuthUser) QUser(com.github.liuweijw.business.admin.domain.QUser) UserBean(com.github.liuweijw.business.admin.beans.UserBean) AuthRole(com.github.liuweijw.system.api.model.AuthRole) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Aggregations

AuthUser (com.github.liuweijw.system.api.model.AuthUser)6 QUser (com.github.liuweijw.business.admin.domain.QUser)3 User (com.github.liuweijw.business.admin.domain.User)3 AuthRole (com.github.liuweijw.system.api.model.AuthRole)3 ArrayList (java.util.ArrayList)3 Cacheable (org.springframework.cache.annotation.Cacheable)2 UserBean (com.github.liuweijw.business.admin.beans.UserBean)1 QRole (com.github.liuweijw.business.admin.domain.QRole)1 QUserRole (com.github.liuweijw.business.admin.domain.QUserRole)1 Role (com.github.liuweijw.business.admin.domain.Role)1 UserRole (com.github.liuweijw.business.admin.domain.UserRole)1 UserDetailsImpl (com.github.liuweijw.system.auth.service.UserDetailsImpl)1 HashSet (java.util.HashSet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 InternalAuthenticationServiceException (org.springframework.security.authentication.InternalAuthenticationServiceException)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1