SpringSecurity学习笔记之 入门 一 (Basic验证方式【没啥人用了】)

依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

补充:
引入以后就能用基本的验证。密码在控制台,账户默认user;

关键接口

public class SecurityConfig extends WebSecurityConfigurerAdapter {

关键方法

方法1:用于配置和新增账户
// 配置账户方法
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //配置一个新账户,配置密码,并且设置一个匹配的路径
        auth.inMemoryAuthentication().withUser("user_admin").password("8888888").authorities("/");
    }
方法2:用于配置访问权限
// 配置拦截规则,认证方式的方法
@Override
protected void configure(HttpSecurity http) throws Exception {
    // 配置所有路径都需要验证,并且  and指定 使用 httpbasic 验证
    http.authorizeRequests().antMatchers("/**").fullyAuthenticated().and().httpBasic();
}

然后我们需要配置一个加密bean

/**
 * 加密bean
 */
@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}
@Bean
    public static NoOpPasswordEncoder passwordEncoder(){
        return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
    }

不然会提示没有配置加密方式

验证时会到内存中对身份进行验证,验证通过以后就会生成一个sessionId返回给浏览器客户端,cookie

这就是基本的方式实现(没啥人用这个了)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值