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>

```









目录
相关文章
|
1月前
|
安全 Java 数据安全/隐私保护
使用Spring Security实现细粒度的权限控制
使用Spring Security实现细粒度的权限控制
|
1月前
|
安全 Java 数据库
实现基于Spring Security的权限管理系统
实现基于Spring Security的权限管理系统
|
1月前
|
安全 Java 数据安全/隐私保护
解析Spring Security中的权限控制策略
解析Spring Security中的权限控制策略
|
2月前
|
JSON 安全 Java
Spring Security 6.x 微信公众平台OAuth2授权实战
上一篇介绍了OAuth2协议的基本原理,以及Spring Security框架中自带的OAuth2客户端GitHub的实现细节,本篇以微信公众号网页授权登录为目的,介绍如何在原框架基础上定制开发OAuth2客户端。
101 4
Spring Security 6.x 微信公众平台OAuth2授权实战
|
2月前
|
存储 安全 Java
Spring Security 6.x OAuth2登录认证源码分析
上一篇介绍了Spring Security框架中身份认证的架构设计,本篇就OAuth2客户端登录认证的实现源码做一些分析。
102 2
Spring Security 6.x OAuth2登录认证源码分析
|
2月前
|
安全 Java API
Spring Security 6.x 图解身份认证的架构设计
【6月更文挑战第1天】本文主要介绍了Spring Security在身份认证方面的架构设计,以及主要业务流程,及核心代码的实现
41 1
Spring Security 6.x 图解身份认证的架构设计
|
1月前
|
安全 Java 数据安全/隐私保护
使用Spring Security实现细粒度的权限控制
使用Spring Security实现细粒度的权限控制
|
1月前
|
安全 Java 数据安全/隐私保护
使用Java和Spring Security实现身份验证与授权
使用Java和Spring Security实现身份验证与授权
|
1月前
|
存储 安全 Java
Spring Security在企业级应用中的应用
Spring Security在企业级应用中的应用
|
2月前
|
存储 安全 Java
Spring Security与OAuth2集成开发
Spring Security与OAuth2集成开发