Springboot 跟 Springsecurity 权限验证,和用户登录

准备工作,导入依赖

   <!--引入thymeleaf依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--        security依赖-->
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

几个简单的页面 目录结构
Springboot 跟 Springsecurity 权限验证,和用户登录

页面的效果
Springboot 跟 Springsecurity 权限验证,和用户登录
写自己的config 类 Springboot 帮我们集成了很多,只需要写一个类,继承WebSecurityConfigurerAdapter 加入注解 @EnableWebSecurity

package com.jj.config;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class Securityconfig extends WebSecurityConfigurerAdapter {
//    重写

    @Override
    protected void configure(HttpSecurity http) throws Exception {
//    设置首页所有人可以访问
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");
//        开启登录 ,没有权限退到登录页
        http.formLogin();
    }
}

效果,当我们点击
Springboot 跟 Springsecurity 权限验证,和用户登录
会退回到登录页面,因为没有权限
Springboot 跟 Springsecurity 权限验证,和用户登录
相当于 Spring 的aop 面向切面。
设置登录的账号密码权限

//    重写登录的账号密码
//    可以是数据库的,我没有连接数据库,就用的内存的

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
//                设置fjj 可以看 vip2 ,vip3
                .withUser("fengjiaojaio").password("123456").roles("vip2","vip3")
//                设置憨憨 只能看vip1
               .and() .withUser("憨憨").password("285").roles("vip1");
    }

版本问题应该会报错,会说密码不安全。可能也是为了大家的密码着想吧,啊哈哈哈哈哈
Springboot 跟 Springsecurity 权限验证,和用户登录

java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
	at org.springframework.security.crypto.password.DelegatingPasswordEncoder$UnmappedIdPasswordEncoder.matches(DelegatingPasswordEncoder.java:254) ~[spring-security-core-5.4.2.jar:5.4.2]
	at org.springframework.security.crypto.password.DelegatingPasswordEncoder.matches(DelegatingPasswordEncoder.java:202) ~[spring-security-core-5.4.2.jar:5.4.2]

** 解决方式一:如果不想设置 密码加密编码的话,可以把版本降低到2.1.X 的 **
** 解决方式二:加上密码的加密 **

//    重写登录的账号密码
//    可以是数据库的,我没有连接数据库,就用的内存的

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
//                设置fjj 可以看 vip2 ,vip3
                .withUser("fengjiaojaio").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
//                设置憨憨 只能看vip1
               .and() .withUser("憨憨").password(new BCryptPasswordEncoder().encode("285")).roles("vip1");
    }

效果
Springboot 跟 Springsecurity 权限验证,和用户登录
Springboot 跟 Springsecurity 权限验证,和用户登录
Springboot 跟 Springsecurity 权限验证,和用户登录
会说没有权限!!
如果连接数据库的话,官方文档给的 ,注入数据源即可!!
Springboot 跟 Springsecurity 权限验证,和用户登录
登录后会有cookie 所以需要开启消除登录的 ,加上这一句话
Springboot 跟 Springsecurity 权限验证,和用户登录
在首页写上注销的按钮
Springboot 跟 Springsecurity 权限验证,和用户登录
这里这个mapper 是Spring security 写好的
点进去
Springboot 跟 Springsecurity 权限验证,和用户登录
下载他的源代码,可以看到注释

Springboot 跟 Springsecurity 权限验证,和用户登录
效果
Springboot 跟 Springsecurity 权限验证,和用户登录
Springboot 跟 Springsecurity 权限验证,和用户登录
有友好的提示
如果不想跳到登录界面,也可以跳到指定的url
Springboot 跟 Springsecurity 权限验证,和用户登录
**Springboot 跟 Springsecurity 权限验证,和用户登录
功能!!

上一篇:SpringSecurity入门到入土教程_1


下一篇:SpringSecurity-1