欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot與SpringSecurity整合方法附源碼

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

依賴

<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{
	
	/**
	 * 授權(quán)
	 */
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		
		//所有人可以訪問首頁,功能頁需要指定權(quán)限才可以訪問
		http.authorizeRequests()
			.antMatchers("/").permitAll()
			.antMatchers("/level1/**").hasRole("vip1")
			.antMatchers("/level2/**").hasRole("vip2")
			.antMatchers("/level3/**").hasRole("vip3");
		
		//沒有權(quán)限將默認(rèn)跳轉(zhuǎn)至登錄頁,需要開啟登錄的頁面
		//loginPage設(shè)置跳轉(zhuǎn)至登錄頁的請(qǐng)求(默認(rèn)為/login)
		//usernameParameter和passwordParameter配置登錄的用戶名和密碼參數(shù)名稱,默認(rèn)就是username和password
		//loginProcessingUrl配置登錄請(qǐng)求的url,需要和表單提交的url一致
		http.formLogin().loginPage("/tologin")
						.usernameParameter("username")
						.passwordParameter("password")
						.loginProcessingUrl("/login");
		//禁用CSRF保護(hù)
		http.csrf().disable();
		//開啟注銷功能和注銷成功后的跳轉(zhuǎn)頁面(默認(rèn)為登錄頁面)
		http.logout().logoutSuccessUrl("/");
		//開啟記住我功能,Cookie默認(rèn)保存兩周
		http.rememberMe().rememberMeParameter("remember");
		
	}
	
	/**
	 * 認(rèn)證
	 */
	@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");
	}
	
}

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

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>注冊(cè)
   </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 等其他頁面:略

運(yùn)行效果:

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

項(xiàng)目源碼:

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

提取碼: nh92

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

相關(guān)文章

  • 關(guān)閉支付寶小額免密支付步驟詳解

    關(guān)閉支付寶小額免密支付步驟詳解

    支付寶現(xiàn)在作為我們?nèi)粘I钪凶畛S玫膽?yīng)用之一,已經(jīng)成為了人們的虛擬錢包。但是最近,有人發(fā)現(xiàn)了支付寶的一個(gè)漏洞,本文將對(duì)如何關(guān)閉小額免密支付進(jìn)行步驟介紹。下面跟著小編一起來看下吧
    2017-01-01
  • Java并發(fā)框架:Executor API詳解

    Java并發(fā)框架:Executor API詳解

    這篇文章主要介紹了Java并發(fā)框架:Executor API詳解,隨著當(dāng)今處理器中可用的核心數(shù)量的增加, 隨著對(duì)實(shí)現(xiàn)更高吞吐量的需求的不斷增長,多線程 API 變得非常流行。 Java 提供了自己的多線程框架,稱為 Executor 框架,需要的朋友可以參考下
    2019-07-07
  • Java Socket編程服務(wù)器響應(yīng)客戶端實(shí)例代碼

    Java Socket編程服務(wù)器響應(yīng)客戶端實(shí)例代碼

    這篇文章主要介紹了Java Socket編程服務(wù)器響應(yīng)客戶端實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2017-12-12
  • Java新手入門學(xué)習(xí)之正則表達(dá)式

    Java新手入門學(xué)習(xí)之正則表達(dá)式

    這篇文章主要給大家介紹了關(guān)于Java新手入門學(xué)習(xí)之正則表達(dá)式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 如何把springboot jar項(xiàng)目 改為war項(xiàng)目

    如何把springboot jar項(xiàng)目 改為war項(xiàng)目

    這篇文章主要介紹了如何把springboot jar項(xiàng)目 改為war項(xiàng)目,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Eclipse中配置Maven build打包的方法步驟

    Eclipse中配置Maven build打包的方法步驟

    這篇文章主要介紹了Eclipse中配置Maven build打包的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Java ForkJoin框架的原理及用法

    Java ForkJoin框架的原理及用法

    這篇文章主要介紹了Java ForkJoin框架的原理及用法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • 淺談Java中的Filter過濾器

    淺談Java中的Filter過濾器

    本篇文章主要介紹了淺談Java中的Filter過濾器,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • SpringBoot采用Dynamic-Datasource方式實(shí)現(xiàn)多JDBC數(shù)據(jù)源

    SpringBoot采用Dynamic-Datasource方式實(shí)現(xiàn)多JDBC數(shù)據(jù)源

    在某些情況下,如果我們需要配置多個(gè)數(shù)據(jù)源,本文主要介紹了SpringBoot采用Dynamic-Datasource方式實(shí)現(xiàn)多JDBC數(shù)據(jù)源,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10
  • Java獲取時(shí)間如何將當(dāng)前時(shí)間減一天、一月、一年、并格式化

    Java獲取時(shí)間如何將當(dāng)前時(shí)間減一天、一月、一年、并格式化

    這篇文章主要介紹了Java獲取時(shí)間,將當(dāng)前時(shí)間減一天、一月、一年,并加以格式化,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-09-09

最新評(píng)論