温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java中如何使用SpringSecurity

发布时间:2021-08-30 09:15:39 来源:亿速云 阅读:96 作者:小新 栏目:开发技术

这篇文章将为大家详细讲解有关java中如何使用SpringSecurity,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

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;
    }
}

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

java中如何使用SpringSecurity

源码分析

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

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

java中如何使用SpringSecurity

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

java中如何使用SpringSecurity

注销及权限控制

 <!--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、根据用户的登录状态进行判断显示该有的信息

java中如何使用SpringSecurity

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

java中如何使用SpringSecurity

java中如何使用SpringSecurity

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

java中如何使用SpringSecurity

6、注销logout-404

java中如何使用SpringSecurity

关于“java中如何使用SpringSecurity”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI