SpringSecurity的配置

一、SpringSecurity的功能简单介绍

(1)简介

SpringSecurity是针对Spring项目的安全框架,也是Spring Boot底层安全模块默认的技术选型,他可以实现强大的Web安全控制,对于安全控制,我们仅需要引入spring-boot-starter-security模块,进行少量的配置,即可实现强大的安全管理!

        主要的几个类:

webSecurityConfigurerAdapter:自定义Security策略

AuthenticationManagerBuilder:自定义的认证策略

@EnableWebSecurity:开启WebSecurity模式

        SpringSecurity的两个主要目标是"认证”和“授权”(访问控制)。

        “认证”(Authentication)

        “授权”(Authorization)

(2)SpringSecurity的主要功能

1.功能权限

2.访问权限

3.菜单权限

4.拦截器,过滤器

5.AOP:横切 配置类

二.SpringSecurity的使用

1.引入Spring Security的依赖包

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

2.配置SecurityConfig类

//AOP : 拦截器
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    //链式编程
    @Override
    protected void configure(HttpSecurity http) throws Exception{
        // super.configure(http);
        //首页所有人可以访问,功能页只有对应权限的人才能访问
        //请求授权的规则
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")          //vip1用户只可以访问该路径下的页面
                .antMatchers("/level2/**").hasRole("vip2")          //vip2用户只可以访问该路径下的页面
                .antMatchers("/level3/**").hasRole("vip3");         //vip3用户只可以访问该路径下的页面

        //没有权限默认回到登录页面,需要开启登录的页面
        http.formLogin();

        //注销,开启了注销功能,跳到首页
        //http.logout().logoutSuccessUrl("/");
        //定制登录页
        http.formLogin().loginPage("/toLogin");

        //开启记住我功能 cooke
        http.rememberMe();
    }

    //认证   springboot 2.1.X 可以直接使用
    //密码编码: passwordEncoder
    //在spring Security 5.0+ 新增了很多的加密方法
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //这些数据正常应该从数据库中读取  现在测试的是从内存中读取的数据
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())   //加了一个密码的编码规则
                .withUser("LJ").password(new BCryptPasswordEncoder().encode("123")).roles("vip2","vip3")
                .and()
                .withUser("root").password(new BCryptPasswordEncoder().encode("123")).roles("vip1","vip2","vip3")
                .and()
                .withUser("guest").password(new BCryptPasswordEncoder().encode("123")).roles("vip1");

        //从数据库中获取数据
//        auth.jdbcAuthentication()
//                .dataSource(dataSource)
//                .withDefaultSchema()
//                .withUser(users.username("user").password("password").roles("USER"))
//                .withUser(users.username("admin").password("password").roles("USER","ADMIN"));
    }
}

三.前端使用SpringSecurity

1.前端使用thymeleaf框架

(1)引入thymeleaf模板的依赖

        <!-- thymeleaf模板 -->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
        </dependency>

(2)引入security-thymeleaf整合包

        <!-- security-thymeleaf整合包 -->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity4</artifactId>
            <version>3.0.4.RELEASE</version>
        </dependency>

(3)前端页面引入semantic-ui的框架包

2.前端页面用户用户认证和授权

如果注销失败:可能的原因是默认开启的csrf的防止网站攻击工具,可以在SecurityConfig中的加入关闭csrf:

 根据登入的人的不同权限,会显示与对应权限的内容。

 

 

 

 

 

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>