Java SpringBoot安全框架整合Spring Security详解

 更新时间:2021年09月14日 09:59:04   作者:DrLai  
这篇文章主要介绍了Spring Boot整合Spring Security的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

1.工业级安全框架介绍

Spring Security基于Spring开发,项目中如果使用Spring作为基础,配合Spring Security做权限更加方便,而Shiro需要和Spring进行整合开发。因此作为spring全家桶中的Spring Security在java领域很常用。

2.建议搭建Spring Security环境

2.1在pom.xml中添加相关依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>springsecurityReview</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <artifactId>spring-boot-dependencies</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.5.4</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>
</project>

2.2创建Handler类

package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class Handler {
    @GetMapping("/index")
    public String index(){
        return "index";
    }
}

2.3创建简单的html和配置相关thymeleaf的路径

2.4最后再加个启动类,那么我们的整合测试就完成勒

package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

2.5成果展示 用户名默认user,密码则随机生成的这串数字

3.进阶版使用

3.1用户名和密码自定义

3.2在config包下创建Encoder

进行密码的校验和转码操作,将密码转成字符串形式,并通过match方法惊醒校验。

3.3赋予账号角色权限

package com.example.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;
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    //角色和资源的关系
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/admin").hasRole("ADMIN")
        .antMatchers("/index").access("hasRole('ADMIN') or hasRole('USER') ")
                .anyRequest().authenticated()
        .and()
        .formLogin()
        .loginPage("/login")
        .permitAll()
        .and()
        .logout()
        .permitAll()
        .and()
        .csrf()
        .disable();
    }
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
       auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder())
       .withUser("user").password(new MyPasswordEncoder()
       .encode("000")).roles("USER")
       .and()
       .withUser("admin").password(new MyPasswordEncoder()
       .encode("123")).roles("ADMIN","USER");
    }
}

最后达到admin账号能访问admin.html和index.html

user只能访问index.html的操作

总结

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

相关文章

  • springboot+Quartz实现任务调度的示例代码

    springboot+Quartz实现任务调度的示例代码

    本篇文章主要介绍了springboot + Quartz 实现任务调度的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • java通过Jsoup爬取网页过程详解

    java通过Jsoup爬取网页过程详解

    这篇文章主要介绍了java通过Jsoup爬取网页过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • MyBatis动态SQL标签用法实例详解

    MyBatis动态SQL标签用法实例详解

    本文通过实例代码给大家介绍了MyBatis动态SQL标签用法,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2017-07-07
  • java使用颜色选择器示例分享

    java使用颜色选择器示例分享

    这篇文章主要介绍了java使用颜色选择器示例,需要的朋友可以参考下
    2014-03-03
  • Java设计模式之原型模式的示例详解

    Java设计模式之原型模式的示例详解

    原型模式(Prototype Pattern)指使用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。本文将通过案例详细讲解一下原型模式,感兴趣的可以了解一下
    2022-02-02
  • JAVA算法起步之快速排序实例

    JAVA算法起步之快速排序实例

    这篇文章主要介绍了JAVA算法起步之快速排序实例,需要的朋友可以参考下
    2014-02-02
  • eclipse创建多层包(多级包)全过程

    eclipse创建多层包(多级包)全过程

    这篇文章主要介绍了eclipse创建多层包(多级包)全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • 最小树形图模板朱刘算法分享

    最小树形图模板朱刘算法分享

    这篇文章主要介绍了最小树形图模板朱刘算法,有需要的朋友可以参考一下
    2014-01-01
  • SpringBoot集成SpringMVC的方法示例

    SpringBoot集成SpringMVC的方法示例

    这篇文章主要介绍了SpringBoot集成SpringMVC的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • RocketMQ整合SpringBoot实现生产级二次封装

    RocketMQ整合SpringBoot实现生产级二次封装

    本文主要介绍了RocketMQ整合SpringBoot实现生产级二次封装,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06

最新评论