广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java SpringBoot安全框架整合Spring Security详解
  • 735
分享到

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

2024-04-02 19:04:59 735人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录1.工业级安全框架介绍2.建议搭建spring Security环境2.1在pom.xml中添加相关依赖2.2创建Handler类2.3创建简单的html和配置相关thymele

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的操作

总结

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

--结束END--

本文标题: Java SpringBoot安全框架整合Spring Security详解

本文链接: https://www.lsjlt.com/news/135592.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作