做网站的公司名字,上海做网站哪个好,淘宝官网首页免费注册,建设银行手机不用了怎么登陆网站前言
前置阅读
spring boot security快速使用示例 spring boot security之前后端分离配置
说明
实际场景#xff0c;我们一般是把用户信息保存在db中#xff08;也可能是调用三方接口#xff09;#xff0c;需要自定义用户信息加载或认证部分的逻辑#xff0c;下面提供…前言
前置阅读
spring boot security快速使用示例 spring boot security之前后端分离配置
说明
实际场景我们一般是把用户信息保存在db中也可能是调用三方接口需要自定义用户信息加载或认证部分的逻辑下面提供一个示例。
代码示例
定义用户bean
AllArgsConstructor
Data
public class User {private String username;private String password;
}定义Mapper
示例代码写死了并不是实际从数据库或某个存储查询用户信息
Component
public class UserMapper {public User select(String username) {return new User(username, pass);}
}定义加载用户数据的类
UserDetailsService 是spring security内置的加载用户信息的接口我们只需要实现这个接口
Slf4j
Component
public class UserDetailsServiceImpl implements UserDetailsService {public static final UserDetails INVALID_USER new org.springframework.security.core.userdetails.User(invalid_user, invalid_password, Collections.emptyList());private final UserMapper userMapper;public UserDetailsServiceImpl(UserMapper userMapper) {this.userMapper userMapper;}Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {// 根据用户名从数据库查询用户信息User user userMapper.select(username);if (user null) {/*** 如果没查询到这个用户考虑两种选择* 1. 返回一个标记无效用户的常量对象* 2. 返回一个不可能认证通过的用户*/return INVALID_USER;
// return new User(username, System.currentTimeMillis() UUID.randomUUID().toString(), Collections.emptyList());}/*** 这里返回的用户密码是否为库里保存的密码是明文/密文取决于认证时密码比对部分的实现每个人的场景不一样* 因为使用的是不加密的PasswordEncoder所以可以返回明文*/return new org.springframework.security.core.userdetails.User(username, user.getPassword(), Collections.emptyList());}
}自定义认证的bean配置
Configuration
public class WebConfiguration {Beanpublic PasswordEncoder passwordEncoder() {// 示例不对密码进行加密处理return NoOpPasswordEncoder.getInstance();}Beanpublic AuthenticationManager authenticationManager(UserDetailsService userDetailsService, PasswordEncoder passwordEncoder) {DaoAuthenticationProvider provider new DaoAuthenticationProvider();// 设置加载用户信息的类provider.setUserDetailsService(userDetailsService);// 比较用户密码的时候密码加密方式provider.setPasswordEncoder(passwordEncoder);return new ProviderManager(Arrays.asList(provider));}}注意因为这个是示例AuthenticationProvider使用的是spring security的DaoAuthenticationProvider 在实际场景中如果不满足可以自定义实现或者继承DaoAuthenticationProvider 重写其中的additionalAuthenticationChecks方法主要就是认证检查的默认实现如下 OverrideSuppressWarnings(deprecation)protected void additionalAuthenticationChecks(UserDetails userDetails,UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {if (authentication.getCredentials() null) {this.logger.debug(Failed to authenticate since no credentials provided);throw new BadCredentialsException(this.messages.getMessage(AbstractUserDetailsAuthenticationProvider.badCredentials, Bad credentials));}String presentedPassword authentication.getCredentials().toString();// 就是比对下请求传过来的密码和根据该用户查询的密码是否一致passwordEncoder是根据不同的加密算法进行加密示例我们用的是NoOpPasswordEncoder也就是原始明文比对if (!this.passwordEncoder.matches(presentedPassword, userDetails.getPassword())) {this.logger.debug(Failed to authenticate since password does not match stored value);throw new BadCredentialsException(this.messages.getMessage(AbstractUserDetailsAuthenticationProvider.badCredentials, Bad credentials));}}
定义登录接口
RequestMapping(/login)
RestController
public class LoginController {private final AuthenticationManager authenticationManager;public LoginController(AuthenticationManager authenticationManager) {this.authenticationManager authenticationManager;}PostMapping()public Object login(RequestBody User user) {try {// 使用定义的AuthenticationManager进行认证处理Authentication authenticate authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword()));// 认证通过设置到当前上下文如果当前认证过程后续还有处理的逻辑需要的话。这个示例是没有必要了SecurityContextHolder.getContext().setAuthentication(authenticate);return login success;}catch (Exception e) {return login failed;}}/*** 获取验证码需要的话可以提供一个验证码获取的接口在上面的login里把验证码传进来进行比对*/GetMapping(/captcha)public Object captcha() {return 1234;}
}
自定义HttpSecurity
Component
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {Overrideprotected void configure(HttpSecurity http) throws Exception {// 在这里自定义配置http.authorizeRequests()// 登录相关接口都允许访问.antMatchers(/login/**).permitAll().anyRequest().authenticated().and().exceptionHandling()// 认证失败返回401状态码前端页面可以根据401状态码跳转到登录页面.authenticationEntryPoint((request, response, authException) -response.sendError(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase())).and().cors()// csrf是否决定禁用请自行考量.and().csrf().disable()// 采用http 的基本认证..httpBasic();}
}测试
示例中用户密码写死是pass 用一个错误的密码试一下响应登录失败 使用正确的密码响应登录成功