Springboot+SpringSecurity权限控制学习笔记(二)HttpBasic模式登录认证

接上篇Springboot+SpringSecurity权限控制学习笔记(一)

HttpBasic模式登录认证

弹出登录对话框,最简单的登录认证方式
新建一个类:auth.WebSecurityConfig

package com.xb.rbac.auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        super.configure(auth);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        super.configure(web);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //super.configure(http);
        /** 开启HttpBasic认证*/
        http.httpBasic()
                //链式编程,返回Http
                .and()
                //认证所有的请求
                .authorizeRequests()
                //任何请求都必须authenticated
                .anyRequest()
                .authenticated();
    }
}

启动项目,并可以看到密码
启动程序,在控制台可以看到显示一串密码
访问url:http://localhost:8080/home.html,将会弹出登录对话框,用户名为user,密码为上面控制台截图显示的密码,登录后即可进入home界面。用户和密码是SpringSecurity自动生成的,我们可以在application.properties中自定义用户名密码:

spring.security.user.name=admin
spring.security.user.password=123456

重新启动项目,即可用此用户名密码访问。这是最简单的用户认证模式。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值