最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot與SpringSecurity整合方法附源碼

 更新時間:2021年01月26日 11:28:23   作者:BLUcoding  
這篇文章主要介紹了SpringBoot與SpringSecurity整合,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

依賴

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<!-- Thymeleaf -->
	<dependency>
		<groupId>org.thymeleaf</groupId>
		<artifactId>thymeleaf-spring5</artifactId>
	</dependency>
	<dependency>
		<groupId>org.thymeleaf.extras</groupId>
		<artifactId>thymeleaf-extras-java8time</artifactId>
	</dependency>
	<!-- SpringSecurity -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-security</artifactId>
	</dependency>
	<!-- Thymeleaf 與 SpringSecurity 整合包 -->
	<dependency>
 		<groupId>org.thymeleaf.extras</groupId>
 		<artifactId>thymeleaf-extras-springsecurity5</artifactId>
 		<version>3.0.4.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
		<exclusions>
			<exclusion>
				<groupId>org.junit.vintage</groupId>
				<artifactId>junit-vintage-engine</artifactId>
			</exclusion>
		</exclusions>
	</dependency>
</dependencies>

Controller:

package com.blu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class RouterController {

	@RequestMapping({ "/", "/index" })
	public String index() {
		return "index";
	}

	@RequestMapping("/tologin")
	public String toLogin() {
		return "views/login";
	}

	@RequestMapping("/level1/{id}")
	public String level1(@PathVariable("id") int id) {
		return "views/level1/" + id;
	}

	@RequestMapping("/level2/{id}")
	public String level2(@PathVariable("id") int id) {
		return "views/level2/" + id;
	}

	@RequestMapping("/level3/{id}")
	public String level3(@PathVariable("id") int id) {
		return "views/level3/" + id;
	}
	
}

SecurityConfig:

package com.blu.config;

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;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
	
	/**
	 * 授權
	 */
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		
		//所有人可以訪問首頁,功能頁需要指定權限才可以訪問
		http.authorizeRequests()
			.antMatchers("/").permitAll()
			.antMatchers("/level1/**").hasRole("vip1")
			.antMatchers("/level2/**").hasRole("vip2")
			.antMatchers("/level3/**").hasRole("vip3");
		
		//沒有權限將默認跳轉至登錄頁,需要開啟登錄的頁面
		//loginPage設置跳轉至登錄頁的請求(默認為/login)
		//usernameParameter和passwordParameter配置登錄的用戶名和密碼參數(shù)名稱,默認就是username和password
		//loginProcessingUrl配置登錄請求的url,需要和表單提交的url一致
		http.formLogin().loginPage("/tologin")
						.usernameParameter("username")
						.passwordParameter("password")
						.loginProcessingUrl("/login");
		//禁用CSRF保護
		http.csrf().disable();
		//開啟注銷功能和注銷成功后的跳轉頁面(默認為登錄頁面)
		http.logout().logoutSuccessUrl("/");
		//開啟記住我功能,Cookie默認保存兩周
		http.rememberMe().rememberMeParameter("remember");
		
	}
	
	/**
	 * 認證
	 */
	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		
		auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
			.withUser("BLU").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
			.and()
			.withUser("root").password(new BCryptPasswordEncoder().encode("111111")).roles("vip1","vip2","vip3")
			.and()
			.withUser("guest").password(new BCryptPasswordEncoder().encode("111222")).roles("vip1");
	}
	
}

注:以上方式認證的用戶和角色信息是存儲在內存中的,在實際開發(fā)中應該從數(shù)據(jù)庫中獲取,詳見:SpringSecurity從數(shù)據(jù)庫中獲取用戶信息進行驗證

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
 <title>首頁</title>
 <!--semantic-ui-->
 <link  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet">
 <link th:href="@{/qinjiang/css/qinstyle.css}" rel="external nofollow" rel="external nofollow" rel="stylesheet">
</head>
<body>

<!--主容器-->
<div class="ui container">

 <div class="ui segment" id="index-header-nav" th:fragment="nav-menu">
  <div class="ui secondary menu">
   <a class="item" th:href="@{/index}" rel="external nofollow" >首頁</a>

   <!--登錄注銷-->
   <div class="right menu">
    <!--如果未登錄-->
 				<div sec:authorize="!isAuthenticated()">
  				<a class="item" th:href="@{/tologin}" rel="external nofollow" >
   				<i class="address card icon"></i> 登錄
  				</a>
 				</div>
    <!--如果已登錄-->
 				<div sec:authorize="isAuthenticated()">
  				<a class="item">
   				<i class="address card icon"></i>
   				用戶名:<span sec:authentication="principal.username"></span>
   				角色:<span sec:authentication="principal.authorities"></span>
  				</a>
 				</div>
 				<div sec:authorize="isAuthenticated()">
  				<a class="item" th:href="@{/logout}" rel="external nofollow" >
   				<i class="address card icon"></i> 注銷
  				</a>
 				</div>
   </div>
  </div>
 </div>

 <div class="ui segment" style="text-align: center">
  <h3>Spring Security Study by BLU</h3>
 </div>

 <div>
  <br>
  <div class="ui three column stackable grid">
   <div class="column" sec:authorize="hasRole('vip1')">
    <div class="ui raised segment">
     <div class="ui">
      <div class="content">
       <h5 class="content">Level 1</h5>
       <hr>
       <div><a th:href="@{/level1/1}" rel="external nofollow" ><i class="bullhorn icon"></i> Level-1-1</a></div>
       <div><a th:href="@{/level1/2}" rel="external nofollow" ><i class="bullhorn icon"></i> Level-1-2</a></div>
       <div><a th:href="@{/level1/3}" rel="external nofollow" ><i class="bullhorn icon"></i> Level-1-3</a></div>
      </div>
     </div>
    </div>
   </div>

   <div class="column" sec:authorize="hasRole('vip2')">
    <div class="ui raised segment">
     <div class="ui">
      <div class="content">
       <h5 class="content">Level 2</h5>
       <hr>
       <div><a th:href="@{/level2/1}" rel="external nofollow" ><i class="bullhorn icon"></i> Level-2-1</a></div>
       <div><a th:href="@{/level2/2}" rel="external nofollow" ><i class="bullhorn icon"></i> Level-2-2</a></div>
       <div><a th:href="@{/level2/3}" rel="external nofollow" ><i class="bullhorn icon"></i> Level-2-3</a></div>
      </div>
     </div>
    </div>
   </div>

   <div class="column" sec:authorize="hasRole('vip3')">
    <div class="ui raised segment">
     <div class="ui">
      <div class="content">
       <h5 class="content">Level 3</h5>
       <hr>
       <div><a th:href="@{/level3/1}" rel="external nofollow" ><i class="bullhorn icon"></i> Level-3-1</a></div>
       <div><a th:href="@{/level3/2}" rel="external nofollow" ><i class="bullhorn icon"></i> Level-3-2</a></div>
       <div><a th:href="@{/level3/3}" rel="external nofollow" ><i class="bullhorn icon"></i> Level-3-3</a></div>
      </div>
     </div>
    </div>
   </div>

  </div>
 </div>
 
</div>


<script th:src="@{/qinjiang/js/jquery-3.1.1.min.js}"></script>
<script th:src="@{/qinjiang/js/semantic.min.js}"></script>

</body>
</html>

views/login.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
 <title>登錄</title>
 <!--semantic-ui-->
 <link  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet">
</head>
<body>

<!--主容器-->
<div class="ui container">

 <div class="ui segment">

  <div style="text-align: center">
   <h1 class="header">登錄</h1>
  </div>

  <div class="ui placeholder segment">
   <div class="ui column very relaxed stackable grid">
    <div class="column">
     <div class="ui form">
      <form th:action="@{/login}" method="post">
       <div class="field">
        <label>Username</label>
        <div class="ui left icon input">
         <input type="text" placeholder="Username" name="username">
         <i class="user icon"></i>
        </div>
       </div>
       <div class="field">
        <label>Password</label>
        <div class="ui left icon input">
         <input type="password" name="password">
         <i class="lock icon"></i>
        </div>
       </div>
       <div class="field">
       	<input type="checkbox" name="remember"> 記住我
       </div>
       
       <input type="submit" class="ui blue submit button"/>
      </form>
     </div>
    </div>
   </div>
  </div>

  <div style="text-align: center">
   <div class="ui label">
    </i>注冊
   </div>
   <br><br>
   <small>736917155@qq.com</small>
  </div>
  <div class="ui segment" style="text-align: center">
   <h3>Spring Security Study by BLU</h3>
  </div>
 </div>


</div>

<script th:src="@{/qinjiang/js/jquery-3.1.1.min.js}"></script>
<script th:src="@{/qinjiang/js/semantic.min.js}"></script>

</body>
</html>

views/level1/1.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
 <title>首頁</title>
 <!--semantic-ui-->
 <link  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet">
 <link th:href="@{/qinjiang/css/qinstyle.css}" rel="external nofollow" rel="external nofollow" rel="stylesheet">
</head>
<body>

<!--主容器-->
<div class="ui container">

 <div th:replace="~{index::nav-menu}"></div>

 <div class="ui segment" style="text-align: center">
  <h3>Level-1-1</h3>
 </div>

</div>

<script th:src="@{/qinjiang/js/jquery-3.1.1.min.js}"></script>
<script th:src="@{/qinjiang/js/semantic.min.js}"></script>

</body>
</html>

views/level2/1.html 等其他頁面:略

運行效果:

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

項目源碼:

鏈接: https://pan.baidu.com/s/1AtbcCht84NT-69-sSUAQRw

提取碼: nh92

到此這篇關于SpringBoot與SpringSecurity整合的文章就介紹到這了,更多相關SpringBoot與SpringSecurity整合內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 詳解Java 中的UnitTest 和 PowerMock

    詳解Java 中的UnitTest 和 PowerMock

    這篇文章主要介紹了Java中的 UnitTest 和 PowerMock,文中講解非常詳細,對大家學習有很大的幫助,感興趣的朋友可以了解下
    2020-06-06
  • SpringBoot緩存Ehcache的使用詳解

    SpringBoot緩存Ehcache的使用詳解

    EhCache、Redis比較常用,使用Redis的時候需要先安裝Redis服務器,本文給大家介紹SpringBoot緩存Ehcache的使用詳解,感興趣的朋友跟隨小編一起看看吧
    2022-03-03
  • maven項目pom.xml中parent標簽的使用小結

    maven項目pom.xml中parent標簽的使用小結

    使用maven是為了更好的幫項目管理包依賴,maven的核心就是pom.xml,當我們需要引入一個jar包時,在pom文件中加上就可以從倉庫中依賴到相應的jar包,本文就來介紹一下maven項目pom.xml中parent標簽的使用小結,感興趣的可以了解一下
    2023-12-12
  • Java垃圾回收器的方法和原理總結

    Java垃圾回收器的方法和原理總結

    本篇文章主要介紹了Java垃圾回收器的方法和原理總結,Java垃圾回收器是Java虛擬機的重要模塊,具有一定的參考價值,有興趣的可以了解一下。
    2016-12-12
  • Java中的升序和降序問題

    Java中的升序和降序問題

    這篇文章主要介紹了Java中的升序和降序問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 使用feign服務調用添加Header參數(shù)

    使用feign服務調用添加Header參數(shù)

    這篇文章主要介紹了使用feign服務調用添加Header參數(shù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java throw和throws使用區(qū)別分析

    Java throw和throws使用區(qū)別分析

    這篇文章主要介紹了Java throw和throws使用區(qū)別分析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05
  • Java基礎知識精通數(shù)組的內存分析

    Java基礎知識精通數(shù)組的內存分析

    數(shù)組對于每一門編程語言來說都是重要的數(shù)據(jù)結構之一,當然不同語言對數(shù)組的實現(xiàn)及處理也不盡相同。Java?語言中提供的數(shù)組是用來存儲固定大小的同類型元素
    2022-04-04
  • Java基礎之刪除文本文件中特定行的內容

    Java基礎之刪除文本文件中特定行的內容

    這篇文章主要介紹了Java基礎之刪除文本文件中特定行的內容,文中有非常詳細的代碼示例,對正在學習java基礎的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • SpringBoot項目整合攔截器詳解

    SpringBoot項目整合攔截器詳解

    這篇文章主要介紹了SpringBoot項目整合攔截器詳解,java里的攔截器是動態(tài)攔截Action調用的對象,它提供了一種機制可以使開發(fā)者在一個Action執(zhí)行的前后執(zhí)行一段代碼,攔截器用于在某個方法或者字段被訪問之前進行攔截,然后再之前或者之后加入某些操作,需要的朋友可以參考下
    2023-10-10

最新評論

阿坝县| 洪江市| 茌平县| 天门市| 江油市| 西宁市| 杨浦区| 万全县| 磐石市| 新乐市| 洮南市| 科技| 黄冈市| 宁波市| 兰西县| 霍林郭勒市| 新平| 长乐市| 大丰市| 吉林省| 高邑县| 平昌县| 仪征市| 吉木萨尔县| 沧源| 德令哈市| 宁德市| 溧水县| 永胜县| 呼玛县| 濮阳县| 黑河市| 汉源县| 顺义区| 新野县| 鹤庆县| 阜新市| 宁远县| 青田县| 阳新县| 景泰县|