All Downloads are FREE. Search and download functionalities are using the official Maven repository.

demo.service.UserDetailsService Maven / Gradle / Ivy

Go to download

使用用户管理脚手架(ums) core 模块基本功能详细的配置: 含anonymous/session简单配置/rememberMe/csrf/登录路由/签到, 不包含session详细配置/验证码/手机登录/权限.

The newest version!
package demo.service;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserCache;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.ServletWebRequest;
import top.dcenter.ums.security.core.api.service.AbstractUserDetailsService;
import top.dcenter.ums.security.core.enums.ErrorCodeEnum;
import top.dcenter.ums.security.core.exception.RegisterUserFailureException;
import top.dcenter.ums.security.core.exception.UserNotExistException;

/**
 *  用户密码与手机短信登录与注册服务:

* 1. 用于第三方登录与手机短信登录逻辑。

* 2. 用于用户密码登录逻辑。

* 3. 用户注册逻辑。

* @author zyw * @version V1.0 Created by 2020/9/20 11:06 */ @Service @Slf4j public class UserDetailsService extends AbstractUserDetailsService { /** * 用户名 */ public static final String PARAM_USERNAME = "username"; /** * 密码 */ public static final String PARAM_PASSWORD = "password"; private final ObjectMapper objectMapper; private final JdbcTemplate jdbcTemplate; @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection") @Autowired(required = false) private UserCache userCache; /** * 用于密码加解密 */ @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection") @Autowired private PasswordEncoder passwordEncoder; public UserDetailsService(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; this.objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); } @SuppressWarnings("AlibabaUndefineMagicConstant") @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { try { // 从缓存中查询用户信息: // 从缓存中查询用户信息 if (this.userCache != null) { UserDetails userDetails = this.userCache.getUserFromCache(username); if (userDetails != null) { return userDetails; } } // 根据用户名获取用户信息 // 获取用户信息逻辑。。。 // ... // 示例:只是从用户登录日志表中提取的信息, log.info("Demo ======>: 登录用户名:{}, 登录成功", username); return new User(username, passwordEncoder.encode("admin"), true, true, true, true, AuthorityUtils.commaSeparatedStringToAuthorityList("admin, ROLE_USER")); } catch (Exception e) { String msg = String.format("Demo ======>: 登录用户名:%s, 登录失败: %s", username, e.getMessage()); log.error(msg, e); throw new UserNotExistException(ErrorCodeEnum.QUERY_USER_INFO_ERROR, e, username); } } @Override public UserDetails registerUser(String mobile) throws RegisterUserFailureException { if (mobile == null) { throw new RegisterUserFailureException(ErrorCodeEnum.MOBILE_NOT_EMPTY, null); } // 用户信息持久化逻辑。。。 // ... log.info("Demo ======>: 手机短信登录用户 {}:注册成功", mobile); User user = new User(mobile, passwordEncoder.encode("admin"), true, true, true, true, AuthorityUtils.commaSeparatedStringToAuthorityList("admin, ROLE_USER") ); // 把用户信息存入缓存 if (userCache != null) { userCache.putUserInCache(user); } return user; } @Override public UserDetails registerUser(ServletWebRequest request) throws RegisterUserFailureException { String username = getValueOfRequest(request, PARAM_USERNAME, ErrorCodeEnum.USERNAME_NOT_EMPTY); String password = getValueOfRequest(request, PARAM_PASSWORD, ErrorCodeEnum.PASSWORD_NOT_EMPTY); // ... // UserInfo userInfo = getUserInfo(request) // 用户信息持久化逻辑。。。 // ... String encodedPassword = passwordEncoder.encode(password); log.info("Demo ======>: 用户名:{}, 注册成功", username); User user = new User(username, encodedPassword, true, true, true, true, AuthorityUtils.commaSeparatedStringToAuthorityList("admin, ROLE_USER") ); // 把用户信息存入缓存 if (userCache != null) { userCache.putUserInCache(user); } return user; } private String getValueOfRequest(ServletWebRequest request, String paramName, ErrorCodeEnum usernameNotEmpty) throws RegisterUserFailureException { String result = request.getParameter(paramName); if (result == null) { throw new RegisterUserFailureException(usernameNotEmpty, request.getSessionId()); } return result; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy