Spring Boot 整合Spring Seccurity

1.创建maven工程

<?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>springsecurity</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<artifactId>spring-boot-starter-web</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.2.4.RELEASE</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-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
</project>

权限管理的开发

权限付给角色,角色付给用户

2.Handler

package com.southwind.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; @Controller
public class SecurtyHandler {
@GetMapping("/index")
public String index(){
return "index";
}
}

3.HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>idnex</p>
<form method="post" action="/login">
<input type="submit" value="退出">
</form>
</body>
</html>

4.配置文件

spring:
thymeleaf:
suffix: .html
prefix: classpath:/templates/

5.启动类

package com;

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

用户名默认user

密码:默认随机

自定义:

spring:
thymeleaf:
suffix: .html
prefix: classpath:/templates/
security:
user:
name: admin
password: 123456

权限管理

定义两个资源:

  • index.html

    package com.southwind.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 {
    /**
    * 角色和资源
    * @param http
    * @throws Exception
    */
    @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();
    } /**
    * 用户和角色
    * @param auth
    * @throws Exception
    */
    @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.html

定义两个角色:

  • ADMIN访问index.html
  • USER访问index.html

1.创建SecurityConfig

2.自定义MypasswordEncoder类

package com.southwind.Config;

import org.springframework.security.crypto.password.PasswordEncoder;

public class MypasswordEncoder implements PasswordEncoder {

    @Override
public String encode(CharSequence charSequence) {
return charSequence.toString();
} @Override
public boolean matches(CharSequence charSequence, String s) {
return s.equals((charSequence.toString()));
}
}
package com.southwind.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 {
/**
* 角色和资源
* @param http
* @throws Exception
*/
@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();
} /**
* 用户和角色
* @param auth
* @throws Exception
*/
@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");
}
}

4.Handler

package com.southwind.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; @Controller
public class SecurtyHandler {
@GetMapping("/index")
public String index(){
return "index";
}
@GetMapping("/admin")
public String admin(){
return "adimin";
}
@GetMapping("/login")
public String login(){
return "login";
}
}

5.HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>后台管理系统</p>
<form method="post" action="/logout">
<input type="submit" value="退出">
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>欢迎回来</p>
<form method="post" action="/logout">
<input type="submit" value="退出">
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:if="%{param.error}">
用户名或密码错误
</p>
<form th:action="@{/login}" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>

SpringBoot 整合Seccurity、权限管理的更多相关文章

  1. SpringBoot&Shiro实现权限管理

    SpringBoot&Shiro实现权限管理 引言 相信大家前来看这篇文章的时候,是有SpringBoot和Shiro基础的,所以本文只介绍整合的步骤,如果哪里写的不好,恳请大家能指出错误,谢 ...

  2. SpringBoot整合Shiro权限框架实战

    什么是ACL和RBAC ACL Access Control list:访问控制列表 优点:简单易用,开发便捷 缺点:用户和权限直接挂钩,导致在授予时的复杂性,比较分散,不便于管理 例子:常见的文件系 ...

  3. SpringBoot + Apache Shiro权限管理

    之前配置过Spring + SpringMVC + JPA + Shiro后台权限管理 + VUE前台登录页面的框架,手动配置各种.xml,比较繁琐,前几天写了个SpringBootShiro的Dem ...

  4. Spring boot整合shiro权限管理

    Apache Shiro功能框架: Shiro聚焦与应用程序安全领域的四大基石:认证.授权.会话管理和保密. #,认证,也叫作登录,用于验证用户是不是他自己所说的那个人: #,授权,也就是访问控制,比 ...

  5. spring-boot-plus集成Shiro+JWT权限管理

    SpringBoot+Shiro+JWT权限管理 Shiro Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理. 使用Shiro的易于理解的API,您可以 ...

  6. springboot(十四):springboot整合shiro-登录认证和权限管理

    这篇文章我们来学习如何使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉及到这方面的需求.在Java领域一般有Spring Security ...

  7. spring-boot(八) springboot整合shiro-登录认证和权限管理

    学习文章:springboot(十四):springboot整合shiro-登录认证和权限管理 Apache Shiro What is Apache Shiro? Apache Shiro是一个功能 ...

  8. springboot(十四):springboot整合shiro-登录认证和权限管理(转)

    springboot(十四):springboot整合shiro-登录认证和权限管理 .embody{ padding:10px 10px 10px; margin:0 -20px; border-b ...

  9. SpringBoot与Shiro整合权限管理实战

    SpringBoot与Shiro整合权限管理实战 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] *观看本文章需要有一定SpringBoot整合经验* Shiro框架简介 Apach ...

  10. springBoot整合spring security实现权限管理(单体应用版)--筑基初期

    写在前面 在前面的学习当中,我们对spring security有了一个小小的认识,接下来我们整合目前的主流框架springBoot,实现权限的管理. 在这之前,假定你已经了解了基于资源的权限管理模型 ...

随机推荐

  1. c++详细学习——继承

    通常讲父类(parrent)-子类(child).基类(base)-派生类(derived)和超类(super)-子类(sub) 1 最基础的写法 以下例子为最基本的写法,默认构造 1 enum Ge ...

  2. Datawhale组队学习_Task02:详读西瓜书+南瓜书第3章

    第3章 线性模型 家人们又来吃瓜了! 3.1 基本形式 线性模型的本质是通过一个所有属性的线性组合进行预测的函数,即 $\mathcal{f(x)=w_1x_1+w_2x_2+...+w_dx_d+b ...

  3. 【PPT】NET Conf China 2022,主题:C#在iNeuOS工业互联网操作系统的开发及应用

    从技术生态发展过程及理念.产品级解决方案理念.产品系统框架及主要功能介绍.产品系统二次开发和应用案例等5个方面进行了主题发言. 从2003到现在,使用.NET技术生态19年左右.  10多年的煤炭.电 ...

  4. JavaEE Day00 Java Web课程介绍

    1.什么是Java Web? 使用Java语言开发互联网项目,简单理解为使用Java语言开发网站 2.课程介绍:30天 1.数据库(5天,第一阶段) 2.静态网页前端(5天,第二阶段) 3.Web核心 ...

  5. 【Java SE进阶】Day08 File类、递归

    一.File类 1.概述java.io.File 文件和路径的抽象表示 用于文件和目录的创建.查找和删除等 分类 file--文件 directory--文件夹/目录 path--路径 2.静态成员变 ...

  6. [数据结构]哈希拉链法化解冲突+ASL计算

    看这篇就好(自用,别骂我) 摘自博客园 存疑:学校考试空节点算不算查找次数???

  7. 体验 Gitea Actions

    即将推出的 Gitea Actions 致力于打造一个 CI/CD 工具的标准协议,第三方 CI 系统可以基于actions 协议与 Gitea 平台集成,提供一站式管理方案.Gitea Action ...

  8. Ubuntu:Docker启动与停止

    安装完成Docker后,默认已经启动了docker服务,如需手动控制docker服务的启停,可执行如下命令 启动docker sudo service docker start 停止docker su ...

  9. 低代码开发平台YonBuilder移动开发,开发阅读APP教程

    ​ 设计实现效果如下图: 主要包括书架,阅读,收藏功能. 经过分析,我们可以先实现底部导航功能,和书架列表页面. 1. 使用 tabLayout 高级窗口实现底部导航 . 使用tabLayout 有两 ...

  10. Solon Java Framework v1.12.0 发布

    一个更现代感的 Java 应用开发框架:更快.更小.更自由.没有 Spring,没有 Servlet,没有 JavaEE:独立的轻量生态.主框架仅 0.1 MB. @Controller public ...