Spring Security实现权限管理(用户、角色、权限)

简介: Spring Security实现权限管理(用户、角色、权限)

项目地址:[Git下载](https://github.com/hnust-xijing/SpringSecurityAuthority.git) 或者  [去项目地址下载](https://github.com/hnust-xijing/SpringSecurityAuthority)

整合spring security

1,创建maven,pom.xml


```xml

<parent>

   <groupId>org.springframework.boot</groupId>

   <artifactId>spring-boot-parent</artifactId>

   <version>2.1.5.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>

```


2,创建Handler


```java

package com.shuang.controller;


import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.GetMapping;


@Controller

public class HelloHandler {


   @GetMapping("/index")

   public String index(){

       return "index";

   }


}

```


3,创建HTML


```html

<!DOCTYPE html>

<html lang="en">

<head>

   <meta charset="UTF-8">

   <title>title</title>

</head>

<body>

   <h1>hello world!</h1>

</body>

</html>

```


4,创建application.yml


```xml

spring:

 thymeleaf:

   prefix: classpath:/templates/

   suffix: .html

```


5,创建启动类Application


```java

package com.shuang;


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

   }

}

```


6,设置自定义密码(springsecurity会默认生成登录界面,默认用户名为:admin,密码控制台会输出)


```xml

spring:

 thymeleaf:

   prefix: classpath:/templates/

   suffix: .html

 security:

   user:

     name: admin

     password: 123123

```


## 权限管理

定义两个HTML资源:index.html、admin.html,同时定义两个角色ADMIN和USER,

ADMIN拥有访问index.html和admin.html的权限,USER只有访问index.html的权限。

7,创建securityConfig类。


```java

package com.shuang.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.EnableWebSecurity;

import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


@Configuration

@EnableWebSecurity

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");

   }

}

```


8,修改Handler


```java

package com.shuang.controller;


import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.GetMapping;


@Controller

public class HelloHandler {


   @GetMapping("/index")

   public String index(){

       return "index";

   }

   @GetMapping("/admin")

   public String admin(){

       return "admin";

   }

   @GetMapping("/login")

   public String login(){

       return "login";

   }

}

```


9,login.html


```html

<!DOCTYPE html>

<html xmlns:th="http://wwww.thymeleaf.org">

<head>

   <meta charset="UTF-8">

   <title>Title</title>

</head>

<body>

   <form th:action="@{/login}" method="post">

       用户名:<input type="text" name="username"/><br/>

       密码:<input type="text" name="password"/><br/>

       <input type="submit" value="登录"/>

   </form>

</body>

</html>

```


10,index.html


```html

<!DOCTYPE html>

<html lang="en">

<head>

   <meta charset="UTF-8">

   <title>title</title>

</head>

<body>

   <h1>hello world!</h1>

   <form action="/logout" method="post">

       <input type="submit" value="退出"/>

   </form>

</body>

</html>

```


11.admin.html


```html

<!DOCTYPE html>

<html lang="en">

<head>

   <meta charset="UTF-8">

   <title>Title</title>

</head>

<body>

   <h1>后台管理系统</h1>

   <form action="/logout" method="post">

       <input type="submit" value="退出">

   </form>

</body>

</html>

```









目录
打赏
0
0
0
0
0
分享
相关文章
什么是JWT?如何使用Spring Boot Security实现它?
什么是JWT?如何使用Spring Boot Security实现它?
1273 5
spring boot 权限管理的几种方式
Spring Boot 提供多种权限管理方式,包括基于角色的访问控制(RBAC)、基于属性的访问控制(ABAC)和基于访问控制列表(ACL)。RBAC 通过角色简化权限管理;ABAC 根据用户、资源和环境属性实现细粒度控制;ACL 则为每个资源定义访问控制列表。文中以 Spring Security 为例,详细展示了每种方法的配置与实现步骤,帮助开发者根据项目需求选择合适的权限管理方案。示例涵盖依赖添加、类配置及注解使用等关键环节。
135 0
|
4月前
|
微服务——SpringBoot使用归纳——Spring Boot中集成 Shiro——Shiro 身份和权限认证
本文介绍了 Apache Shiro 的身份认证与权限认证机制。在身份认证部分,分析了 Shiro 的认证流程,包括应用程序调用 `Subject.login(token)` 方法、SecurityManager 接管认证以及通过 Realm 进行具体的安全验证。权限认证部分阐述了权限(permission)、角色(role)和用户(user)三者的关系,其中用户可拥有多个角色,角色则对应不同的权限组合,例如普通用户仅能查看或添加信息,而管理员可执行所有操作。
151 0
Spring Security 6.x 微信公众平台OAuth2授权实战
上一篇介绍了OAuth2协议的基本原理,以及Spring Security框架中自带的OAuth2客户端GitHub的实现细节,本篇以微信公众号网页授权登录为目的,介绍如何在原框架基础上定制开发OAuth2客户端。
443 4
Spring Security 6.x 微信公众平台OAuth2授权实战
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问