Search in sources :

Example 1 with User

use of com.alibaba.nacos.config.server.model.User in project XHuiCloud by sindaZeng.

the class UserController method updatePassword.

/**
 * Update password.
 * @param oldPassword old password
 * @param newPassword new password
 * @return Code 200 if update successfully, Code 401 if old password invalid,
 * otherwise 500
 */
@PutMapping("/password")
@Deprecated
public RestResult<String> updatePassword(@RequestParam(value = "oldPassword") String oldPassword, @RequestParam(value = "newPassword") String newPassword) {
    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    String username = ((UserDetails) principal).getUsername();
    User user = userDetailsService.getUserFromDatabase(username);
    String password = user.getPassword();
    // TODO: throw out more fine grained exceptions
    try {
        if (PasswordEncoderUtil.matches(oldPassword, password)) {
            userDetailsService.updateUserPassword(username, PasswordEncoderUtil.encode(newPassword));
            return RestResultUtils.success("Update password success");
        }
        return RestResultUtils.failed(HttpStatus.UNAUTHORIZED.value(), "Old password is invalid");
    } catch (Exception e) {
        return RestResultUtils.failed(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Update userpassword failed");
    }
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) NacosUser(com.alibaba.nacos.security.nacos.users.NacosUser) User(com.alibaba.nacos.config.server.model.User) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) AccessException(com.alibaba.nacos.auth.exception.AccessException) IOException(java.io.IOException)

Example 2 with User

use of com.alibaba.nacos.config.server.model.User in project XHuiCloud by sindaZeng.

the class LdapAuthenticationProvider method authenticate.

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String username = (String) authentication.getPrincipal();
    String password = (String) authentication.getCredentials();
    if (isAdmin(username)) {
        UserDetails userDetails = userDetailsService.loadUserByUsername(username);
        if (PasswordEncoderUtil.matches(password, userDetails.getPassword())) {
            return new UsernamePasswordAuthenticationToken(userDetails, password, userDetails.getAuthorities());
        } else {
            return null;
        }
    }
    if (!ldapLogin(username, password)) {
        return null;
    }
    UserDetails userDetails;
    try {
        userDetails = userDetailsService.loadUserByUsername(LDAP_PREFIX + username);
    } catch (UsernameNotFoundException exception) {
        String nacosPassword = PasswordEncoderUtil.encode(DEFAULT_PASSWORD);
        userDetailsService.createUser(LDAP_PREFIX + username, nacosPassword);
        User user = new User();
        user.setUsername(LDAP_PREFIX + username);
        user.setPassword(nacosPassword);
        userDetails = new NacosUserDetails(user);
    }
    return new UsernamePasswordAuthenticationToken(userDetails, password, userDetails.getAuthorities());
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) NacosUserDetails(com.alibaba.nacos.security.nacos.users.NacosUserDetails) UserDetails(org.springframework.security.core.userdetails.UserDetails) User(com.alibaba.nacos.config.server.model.User) NacosUserDetails(com.alibaba.nacos.security.nacos.users.NacosUserDetails) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 3 with User

use of com.alibaba.nacos.config.server.model.User in project XHuiCloud by sindaZeng.

the class NacosUserDetailsServiceImpl method reload.

@Scheduled(initialDelay = 5000, fixedDelay = 15000)
private void reload() {
    try {
        Page<User> users = getUsersFromDatabase(1, Integer.MAX_VALUE);
        if (users == null) {
            return;
        }
        Map<String, User> map = new ConcurrentHashMap<>(16);
        for (User user : users.getPageItems()) {
            map.put(user.getUsername(), user);
        }
        userMap = map;
    } catch (Exception e) {
        Loggers.AUTH.warn("[LOAD-USERS] load failed", e);
    }
}
Also used : User(com.alibaba.nacos.config.server.model.User) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 4 with User

use of com.alibaba.nacos.config.server.model.User in project nacos by alibaba.

the class UserController method createUser.

/**
 * Create a new user.
 *
 * @param username username
 * @param password password
 * @return ok if create succeed
 * @throws IllegalArgumentException if user already exist
 * @since 1.2.0
 */
@Secured(resource = NacosAuthConfig.CONSOLE_RESOURCE_NAME_PREFIX + "users", action = ActionTypes.WRITE)
@PostMapping
public Object createUser(@RequestParam String username, @RequestParam String password) {
    User user = userDetailsService.getUserFromDatabase(username);
    if (user != null) {
        throw new IllegalArgumentException("user '" + username + "' already exist!");
    }
    userDetailsService.createUser(username, PasswordEncoderUtil.encode(password));
    return RestResultUtils.success("create user ok!");
}
Also used : NacosUser(com.alibaba.nacos.console.security.nacos.users.NacosUser) User(com.alibaba.nacos.config.server.model.User) PostMapping(org.springframework.web.bind.annotation.PostMapping) Secured(com.alibaba.nacos.auth.annotation.Secured)

Example 5 with User

use of com.alibaba.nacos.config.server.model.User in project nacos by alibaba.

the class UserController method updatePassword.

/**
 * Update password.
 *
 * @param oldPassword old password
 * @param newPassword new password
 * @return Code 200 if update successfully, Code 401 if old password invalid, otherwise 500
 */
@PutMapping("/password")
@Deprecated
public RestResult<String> updatePassword(@RequestParam(value = "oldPassword") String oldPassword, @RequestParam(value = "newPassword") String newPassword) {
    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    String username = ((UserDetails) principal).getUsername();
    User user = userDetailsService.getUserFromDatabase(username);
    String password = user.getPassword();
    // TODO: throw out more fine grained exceptions
    try {
        if (PasswordEncoderUtil.matches(oldPassword, password)) {
            userDetailsService.updateUserPassword(username, PasswordEncoderUtil.encode(newPassword));
            return RestResultUtils.success("Update password success");
        }
        return RestResultUtils.failed(HttpStatus.UNAUTHORIZED.value(), "Old password is invalid");
    } catch (Exception e) {
        return RestResultUtils.failed(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Update userpassword failed");
    }
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) NacosUser(com.alibaba.nacos.console.security.nacos.users.NacosUser) User(com.alibaba.nacos.config.server.model.User) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) AccessException(com.alibaba.nacos.auth.exception.AccessException) IOException(java.io.IOException) PutMapping(org.springframework.web.bind.annotation.PutMapping)

Aggregations

User (com.alibaba.nacos.config.server.model.User)12 Secured (com.alibaba.nacos.auth.annotation.Secured)4 UserDetails (org.springframework.security.core.userdetails.UserDetails)4 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)4 NacosUser (com.alibaba.nacos.console.security.nacos.users.NacosUser)3 NacosUser (com.alibaba.nacos.security.nacos.users.NacosUser)3 AccessException (com.alibaba.nacos.auth.exception.AccessException)2 IOException (java.io.IOException)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Test (org.junit.Test)2 Scheduled (org.springframework.scheduling.annotation.Scheduled)2 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 PutMapping (org.springframework.web.bind.annotation.PutMapping)2 NacosUserDetails (com.alibaba.nacos.console.security.nacos.users.NacosUserDetails)1 NacosUserDetails (com.alibaba.nacos.security.nacos.users.NacosUserDetails)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1