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:

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

 

 

 

 

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Security 是一个强大且灵活的框架,用于在 Spring 应用程序中进行身份验证和授权管理。下面是一个简单的 Spring Security 配置示例: 1. 添加 Maven 依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 2. 创建一个继承自 `WebSecurityConfigurerAdapter` 的配置类,并重写 `configure` 方法: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/public").permitAll() // 公共资源不需要身份认证 .anyRequest().authenticated() // 其他请求需要认证 .and() .formLogin() // 使用表单登录 .and() .logout().logoutSuccessUrl("/login").permitAll(); // 登出后跳转到登录页 } } ``` 在上述配置中: - `authorizeRequests()` 定义了哪些请求需要进行身份认证。 - `antMatchers()` 指定了不需要身份认证的请求路径。 - `anyRequest().authenticated()` 表示其他请求都需要进行认证。 - `formLogin()` 开启表单登录方式。 - `logout()` 配置了登出功能。 3. 添加用户认证配置: ```java @Configuration public class UserConfig extends WebSecurityConfigurerAdapter { @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("user").password("{noop}password").roles("USER") .and() .withUser("admin").password("{noop}password").roles("USER", "ADMIN"); } } ``` 这里使用了内存认证,定义了两个用户:user 和 admin,密码都是 password,并分别赋予了不同的角色。 以上是一个简单的 Spring Security 配置示例,你可以根据自己的业务需求进行更详细的配置和定制化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值