java SpringSecurity使用详解

 更新时间:2021年08月31日 16:55:40   作者:你好y  
这篇文章主要介绍了java中Spring Security的实例详解的相关资料,spring security是一个多方面的安全认证框架,提供了基于JavaEE规范的完整的安全认证解决方案,需要的朋友可以参考下

SpringSecurity

shrio,SpringSecurity:认证,授权(VIP1,vip2…)

  • 功能权限
  • 访问权限
  • 菜单权限
  • 拦截器,过滤器:大量的原生代码,冗余

1、pom.xml

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

简介

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

  • WebSecurityConfigurerAdapter: 自定义Security策略
  • AuthenticationManagerBuilder:自定义认证策略
  • @EnableWebSecurity: 开启WebSecurity模式 @Enablexxxx 开启某个功能

Spring Security的两个主要目标是“认证”和“授权”(访问控制) .

“认证”(Authentication)
“授权”(Authorization)
这个概念是通用的,而不是只在Spring Security中存在。

1、pom.xml

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

2、Security的controller

package com.kuang.config;
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.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
/*权限验证的配置类,要先继承WebSecurityConfigurerAdapter*/
@Configuration
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");
        //没有权限默认跳转到登陆页面  /login
        http.formLogin();
    }
    //认证
    /* 密码编码: BCryptPasswordEncoder()
    不编码会报下面的错误
    * java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
    * */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("qin").password(new BCryptPasswordEncoder().encode("111")).roles("vip1","vip2","vip3")
                .and()
                .withUser("aaa").password(new BCryptPasswordEncoder().encode("111")).roles("vip1");
    }
}

3、路径转发的controller

package com.kuang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Controller
public class PathController {
    @GetMapping({"/","index"})    //"/""index"都会去到index.html
    public String Toindex(){
        return "index";
    }
    @GetMapping("/toLogin")
    public String Tologin(){
        return "views/login";
    }
    @GetMapping("/level1/{id}")    //@PathVariable获得url的占位符里面的值
    public String ToView(@PathVariable("id")int id){
        return "views/level1/"+id;
    }
    @GetMapping("/level2/{id}")
    public String ToView2(@PathVariable("id")int id){
        return "views/level2/"+id;
    }
    @GetMapping("/level3/{id}")
    public String ToView3(@PathVariable("id")int id){
        return "views/level3/"+id;
    }
}

当然也可以在数据库中拿信息

源码分析

没有权限的话会自动转发到/login

//没有权限默认跳转到登陆页面 /login
http.formLogin();

//开启注销
http.logout();

注销及权限控制

 <!--thymeleof整合security-->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>
xmlns:sec=http://www.thymeleaf.org/extras/spring-security  

用spring-security实现用户登录后显示用户角色的信息

1、导入依赖thymeleof整合security

 <!--thymeleof整合security-->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>

2、html命名空间

<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">

3、根据用户的登录状态进行判断显示该有的信息

4、根据源码写表单name属性

5、实现有什么权限显示什么样的信息

6、注销logout-404

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!

相关文章

  • JavaWeb Refresh响应头代码实例详解

    JavaWeb Refresh响应头代码实例详解

    这篇文章主要介绍了JavaWeb Refresh响应头代码实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • 搞懂JAVAObject中的hashCode()

    搞懂JAVAObject中的hashCode()

    今天小编就为大家分享一篇关于关于Object中equals方法和hashCode方法判断的分析,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2021-08-08
  • SpringBoot利用Validation包实现高效参数校验

    SpringBoot利用Validation包实现高效参数校验

    如果不进行校验就直接使用这些数据,可能会导致各种问题,那么SpringBoot如何利用Validation包实现高效参数校验呢,下面让我们一起来探讨这个重要的话题吧
    2025-04-04
  • IDEA 自定义方法注解模板的实现方法

    IDEA 自定义方法注解模板的实现方法

    这篇文章主要介绍了IDEA 自定义方法注解模板的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09
  • mybatis中的mapper.xml使用循环语句

    mybatis中的mapper.xml使用循环语句

    这篇文章主要介绍了mybatis中的mapper.xml使用循环语句,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • Java常用工具类库——Hutool的使用简介

    Java常用工具类库——Hutool的使用简介

    这篇文章主要介绍了Java常用工具类库——Hutool的使用简介,帮助大家更好的理解和学习使用Java,感兴趣的朋友可以了解下
    2021-04-04
  • Java使用双异步实现将Excel的数据导入数据库

    Java使用双异步实现将Excel的数据导入数据库

    在开发中,我们经常会遇到这样的需求,将Excel的数据导入数据库中,这篇文章主要来和大家讲讲Java如何使用双异步实现将Excel的数据导入数据库,感兴趣的可以了解下
    2024-01-01
  • springboot 在idea中实现热部署的方法

    springboot 在idea中实现热部署的方法

    这篇文章主要介绍了springboot 在idea中实现热部署的方法,实现了热部署,在每一次作了修改之后,都会自动的重启,非常节约时间,感兴趣的小伙伴们可以参考一下
    2018-10-10
  • springboot shardingjdbc与druid数据源冲突问题及解决

    springboot shardingjdbc与druid数据源冲突问题及解决

    这篇文章主要介绍了springboot shardingjdbc与druid数据源冲突问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-06-06
  • java FileWriter 追加文件及文件改名方式

    java FileWriter 追加文件及文件改名方式

    这篇文章主要介绍了java FileWriter 追加文件及文件改名的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07

最新评论