当前位置:   article > 正文

Spring Boot Sercurity---用户登录时认证的三种方法_springboot登录页面实现用户认证

springboot登录页面实现用户认证

1.第一种,直接在yml文件中进行设置

2.第二种,直接在内存中匹配用户名和密码

首先我们要创建一个Sercurity的配置类,在配置类中实现config方法方法,

  1. package com.example.sercurity.config;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  4. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  5. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  6. @Configuration
  7. public class SercurityConfig extends WebSecurityConfigurerAdapter {
  8. @Override
  9. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  10. //采用 BCryptPasswordEncoder方法对密码进行加密
  11. BCryptPasswordEncoder encoder = new BCryptPasswordEncoder ( );
  12. String encode = encoder.encode ( "123456" );
  13. //inMemoryAuthentication方法是对用户登录进行请求认证
  14. auth.inMemoryAuthentication ().withUser ( "admin" ).password ( encode ).roles ( "vip1" );
  15. //super.configure ( auth );
  16. }
  17. }

此时运行,当你输入账号密码时后报错,因为缺少一个PasswordEncoder对象,错误如下:

 此时我们需要创建一个能返回PasswordEncoder对象的Bean类

  1. @Bean
  2. PasswordEncoder password(){
  3. return new BCryptPasswordEncoder();
  4. }

3.第三种,通过已定义的配置类连接数据库进行认证

具体请看此博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/53891
推荐阅读
相关标签
  

闽ICP备14008679号