Search in sources :

Example 1 with AccessException

use of com.alibaba.nacos.auth.exception.AccessException in project XHuiCloud by sindaZeng.

the class NacosAuthManager method resolveTokenFromUser.

private String resolveTokenFromUser(String userName, String rawPassword) throws AccessException {
    String finalName;
    Authentication authenticate;
    try {
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userName, rawPassword);
        authenticate = authenticationManager.authenticate(authenticationToken);
    } catch (AuthenticationException e) {
        throw new AccessException("unknown user!");
    }
    if (null == authenticate || StringUtils.isBlank(authenticate.getName())) {
        finalName = userName;
    } else {
        finalName = authenticate.getName();
    }
    return tokenManager.createToken(finalName);
}
Also used : AccessException(com.alibaba.nacos.auth.exception.AccessException) AuthenticationException(org.springframework.security.core.AuthenticationException) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 2 with AccessException

use of com.alibaba.nacos.auth.exception.AccessException in project XHuiCloud by sindaZeng.

the class NacosAuthManager method loginRemote.

@Override
public User loginRemote(Object request) throws AccessException {
    Request req = (Request) request;
    String token = resolveToken(req);
    if (StringUtils.isBlank(token)) {
        throw new AccessException("user not found!");
    }
    try {
        tokenManager.validateToken(token);
    } catch (ExpiredJwtException e) {
        throw new AccessException("token expired!");
    } catch (Exception e) {
        throw new AccessException("token invalid!");
    }
    Authentication authentication = tokenManager.getAuthentication(token);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    String username = authentication.getName();
    NacosUser user = new NacosUser();
    user.setUserName(username);
    user.setToken(token);
    List<RoleInfo> roleInfoList = roleService.getRoles(username);
    if (roleInfoList != null) {
        for (RoleInfo roleInfo : roleInfoList) {
            if (roleInfo.getRole().equals(NacosRoleServiceImpl.GLOBAL_ADMIN_ROLE)) {
                user.setGlobalAdmin(true);
                break;
            }
        }
    }
    return user;
}
Also used : AccessException(com.alibaba.nacos.auth.exception.AccessException) ExpiredJwtException(io.jsonwebtoken.ExpiredJwtException) NacosUser(com.alibaba.nacos.security.nacos.users.NacosUser) RoleInfo(com.alibaba.nacos.config.server.auth.RoleInfo) Authentication(org.springframework.security.core.Authentication) Request(com.alibaba.nacos.api.remote.request.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) AccessException(com.alibaba.nacos.auth.exception.AccessException) ExpiredJwtException(io.jsonwebtoken.ExpiredJwtException) AuthenticationException(org.springframework.security.core.AuthenticationException)

Example 3 with AccessException

use of com.alibaba.nacos.auth.exception.AccessException in project nacos by alibaba.

the class NacosAuthManager method resolveTokenFromUser.

private String resolveTokenFromUser(String userName, String rawPassword) throws AccessException {
    String finalName;
    Authentication authenticate;
    try {
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userName, rawPassword);
        authenticate = authenticationManager.authenticate(authenticationToken);
    } catch (AuthenticationException e) {
        throw new AccessException("unknown user!");
    }
    if (null == authenticate || StringUtils.isBlank(authenticate.getName())) {
        finalName = userName;
    } else {
        finalName = authenticate.getName();
    }
    return tokenManager.createToken(finalName);
}
Also used : AccessException(com.alibaba.nacos.auth.exception.AccessException) AuthenticationException(org.springframework.security.core.AuthenticationException) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 4 with AccessException

use of com.alibaba.nacos.auth.exception.AccessException in project nacos by alibaba.

the class NacosAuthManager method login.

@Override
public User login(Object request) throws AccessException {
    HttpServletRequest req = (HttpServletRequest) request;
    String token = resolveToken(req);
    if (StringUtils.isBlank(token)) {
        throw new AccessException("user not found!");
    }
    try {
        tokenManager.validateToken(token);
    } catch (ExpiredJwtException e) {
        throw new AccessException("token expired!");
    } catch (Exception e) {
        throw new AccessException("token invalid!");
    }
    Authentication authentication = tokenManager.getAuthentication(token);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    String username = authentication.getName();
    NacosUser user = new NacosUser();
    user.setUserName(username);
    user.setToken(token);
    List<RoleInfo> roleInfoList = roleService.getRoles(username);
    if (roleInfoList != null) {
        for (RoleInfo roleInfo : roleInfoList) {
            if (roleInfo.getRole().equals(NacosRoleServiceImpl.GLOBAL_ADMIN_ROLE)) {
                user.setGlobalAdmin(true);
                break;
            }
        }
    }
    req.getSession().setAttribute(RequestUtil.NACOS_USER_KEY, user);
    return user;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) AccessException(com.alibaba.nacos.auth.exception.AccessException) ExpiredJwtException(io.jsonwebtoken.ExpiredJwtException) NacosUser(com.alibaba.nacos.console.security.nacos.users.NacosUser) RoleInfo(com.alibaba.nacos.config.server.auth.RoleInfo) Authentication(org.springframework.security.core.Authentication) AccessException(com.alibaba.nacos.auth.exception.AccessException) ExpiredJwtException(io.jsonwebtoken.ExpiredJwtException) AuthenticationException(org.springframework.security.core.AuthenticationException)

Example 5 with AccessException

use of com.alibaba.nacos.auth.exception.AccessException in project nacos by alibaba.

the class NacosAuthManager method loginRemote.

@Override
public User loginRemote(Object request) throws AccessException {
    Request req = (Request) request;
    String token = resolveToken(req);
    if (StringUtils.isBlank(token)) {
        throw new AccessException("user not found!");
    }
    try {
        tokenManager.validateToken(token);
    } catch (ExpiredJwtException e) {
        throw new AccessException("token expired!");
    } catch (Exception e) {
        throw new AccessException("token invalid!");
    }
    Authentication authentication = tokenManager.getAuthentication(token);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    String username = authentication.getName();
    NacosUser user = new NacosUser();
    user.setUserName(username);
    user.setToken(token);
    List<RoleInfo> roleInfoList = roleService.getRoles(username);
    if (roleInfoList != null) {
        for (RoleInfo roleInfo : roleInfoList) {
            if (roleInfo.getRole().equals(NacosRoleServiceImpl.GLOBAL_ADMIN_ROLE)) {
                user.setGlobalAdmin(true);
                break;
            }
        }
    }
    return user;
}
Also used : AccessException(com.alibaba.nacos.auth.exception.AccessException) ExpiredJwtException(io.jsonwebtoken.ExpiredJwtException) NacosUser(com.alibaba.nacos.console.security.nacos.users.NacosUser) RoleInfo(com.alibaba.nacos.config.server.auth.RoleInfo) Authentication(org.springframework.security.core.Authentication) Request(com.alibaba.nacos.api.remote.request.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) AccessException(com.alibaba.nacos.auth.exception.AccessException) ExpiredJwtException(io.jsonwebtoken.ExpiredJwtException) AuthenticationException(org.springframework.security.core.AuthenticationException)

Aggregations

AccessException (com.alibaba.nacos.auth.exception.AccessException)6 Authentication (org.springframework.security.core.Authentication)6 AuthenticationException (org.springframework.security.core.AuthenticationException)6 RoleInfo (com.alibaba.nacos.config.server.auth.RoleInfo)4 ExpiredJwtException (io.jsonwebtoken.ExpiredJwtException)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 Request (com.alibaba.nacos.api.remote.request.Request)2 NacosUser (com.alibaba.nacos.console.security.nacos.users.NacosUser)2 NacosUser (com.alibaba.nacos.security.nacos.users.NacosUser)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2