日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

基于Spring Boot保護Web應用程序

瀏覽:27日期:2023-05-29 10:09:44

如果在類路徑上添加了Spring Boot Security依賴項,則Spring Boot應用程序會自動為所有HTTP端點提供基本身份驗證。端點“/”和“/home”不需要任何身份驗證。所有其他端點都需要身份驗證。

要將Spring Boot Security添加到Spring Boot應用程序,需要在構建配置文件中添加Spring Boot Starter Security依賴項。

Maven用戶可以在pom.xml 文件中添加以下依賴項。

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId></dependency>

XML

Gradle用戶可以在build.gradle 文件中添加以下依賴項。

compile('org.springframework.boot:spring-boot-starter-security')

保護Web應用程序

首先,使用Thymeleaf模板創建不安全的Web應用程序。

然后,在 src/main/resources/templates 目錄下創建一個home.html 文件。

<!DOCTYPE html><html xmlns = 'http://www.w3.org/1999/xhtml' xmlns:th = 'http://www.thymeleaf.org' xmlns:sec = 'http://www.thymeleaf.org/thymeleaf-extras-springsecurity3'> <head> <title>Spring Security示例</title> </head> <body> <h1>歡迎您!</h1> <p>點擊 <a th:href = 'http://www.b3g6.com/bcjs/@{/hello}'>這里</a> 看到問候語.</p> </body></html>

HTML

使用Thymeleaf模板在HTML文件中定義的簡單視圖/hello。現在,在src/main/resources/templates目錄下創建一個文件:hello.html。

<!DOCTYPE html><html xmlns = 'http://www.w3.org/1999/xhtml' xmlns:th = 'http://www.thymeleaf.org' xmlns:sec = 'http://www.thymeleaf.org/thymeleaf-extras-springsecurity3'> <head> <title>Hello World!</title> </head> <body> <h1>Hello world!</h1> </body></html>

HTML

現在,需要為Home和hello視圖設置Spring MVC - View控制器。為此,創建一個擴展WebMvcConfigurerAdapter的MVC配置文件。

package com.yiibai.websecuritydemo;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@Configurationpublic class MvcConfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController('/home').setViewName('home'); registry.addViewController('/').setViewName('home'); registry.addViewController('/hello').setViewName('hello'); registry.addViewController('/login').setViewName('login'); }}

Java

現在,將Spring Boot Starter安全依賴項添加到構建配置文件中。Maven用戶可以在pom.xml 文件中添加以下依賴項。

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId></dependency>

XML

Gradle用戶可以在build.gradle 文件中添加以下依賴項。

compile('org.springframework.boot:spring-boot-starter-security')

現在,創建一個Web安全配置文件,該文件用于保護應用程序以使用基本身份驗證訪問HTTP端點。

package com.yiibai.websecuritydemo;import org.springframework.beans.factory.annotation.Autowired;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;import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;@Configuration@EnableWebSecuritypublic class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers('/', '/home').permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage('/login') .permitAll() .and() .logout() .permitAll(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser('user').password('password').roles('USER'); }}

Java

現在,在src/main/resources 目錄下創建一個login.html 文件,以允許用戶通過登錄屏幕訪問HTTP端點。

<!DOCTYPE html><html xmlns = 'http://www.w3.org/1999/xhtml' xmlns:th = 'http://www.thymeleaf.org' xmlns:sec = 'http://www.thymeleaf.org/thymeleaf-extras-springsecurity3'> <head> <title>Spring Security示例</title> </head> <body> <div th:if = '${param.error}'> 無效的用戶名和密碼. </div> <div th:if = '${param.logout}'> 你已經注銷. </div> <form th:action = '@{/login}' method = 'post'> <div> <label> 用戶名 : <input type = 'text' name = 'username'/> </label> </div> <div> <label> 密碼: <input type = 'password' name = 'password'/> </label> </div> <div> <input type = 'submit' value = '登錄'/> </div> </form> </body></html>

HTML

最后,更新hello.html 文件 - 允許用戶從應用程序注銷并顯示當前用戶名,如下所示 -

<!DOCTYPE html><html xmlns = 'http://www.w3.org/1999/xhtml' xmlns:th = 'http://www.thymeleaf.org' xmlns:sec = 'http://www.thymeleaf.org/thymeleaf-extras-springsecurity3'> <head> <title>Hello World!</title> </head> <body> <h1 th:inline = 'text'>您好,[[${#httpServletRequest.remoteUser}]]!</h1> <form th:action = '@{/logout}' method = 'post'> <input type = 'submit' value = '注銷'/> </form> </body></html>

HTML

主 Spring Boot應用程序的代碼如下 -

package com.yiibai.websecuritydemo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class WebsecurityDemoApplication { public static void main(String[] args) { SpringApplication.run(WebsecurityDemoApplication.class, args); }}

Java

下面給出了構建配置文件的完整代碼。

Maven構建文件 - pom.xml 的內容如下:

<?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>com.yiibai</groupId> <artifactId>websecurity-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>websecurity-demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>

XML

Gradle構建文件 ? build.gradle

buildscript { ext { springBootVersion = ‘1.5.9.RELEASE‘ } repositories { mavenCentral() } dependencies { classpath('org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}') }}apply plugin: ‘java‘apply plugin: ‘eclipse‘apply plugin: ‘org.springframework.boot‘group = ‘com.yiibai‘version = ‘0.0.1-SNAPSHOT‘sourceCompatibility = 1.8repositories { mavenCentral()}dependencies { compile(‘org.springframework.boot:spring-boot-starter-security‘) compile(‘org.springframework.boot:spring-boot-starter-thymeleaf‘) compile(‘org.springframework.boot:spring-boot-starter-web‘) testCompile(‘org.springframework.boot:spring-boot-starter-test‘) testCompile(‘org.springframework.security:spring-security-test‘)}

現在,創建一個可執行的JAR文件,并使用以下Maven或Gradle命令運行Spring Boot應用程序。

Maven用戶請使用下面給出的命令 -

mvn clean install

Shell

在“BUILD SUCCESS”之后,可以在target目錄下找到JAR文件。Gradle用戶可以使用如下所示的命令 -

gradle clean build

在“BUILD SUCCESSFUL”之后,可以在build/libs 目錄下找到JAR文件。

現在,使用下面顯示的命令運行JAR文件 -

java ?jar <JARFILE>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
国产 日韩 欧美 综合 一区| 日韩在线免费| 欧美国产小视频| 国产一区二区三区不卡av| 亚洲免费播放| 日韩中文欧美| 久久国产亚洲| 欧美日韩国产在线一区| 国产精品久久久久久久久久齐齐| 欧美freesex黑人又粗又大| 国产精品成人国产| 国产欧美日韩精品一区二区免费 | 国产亚洲欧洲| 亚洲一区二区三区高清不卡| 亚洲一区二区三区四区五区午夜| 国产成人精品三级高清久久91| 一区在线观看| 欧美69视频| 国产视频一区三区| 少妇高潮一区二区三区99| 亚洲精品乱码| 日本免费新一区视频| 国产精品国码视频| 国产粉嫩在线观看| 欧美日韩国产高清电影| 欧美精品99| 欧美aaaaaa午夜精品| 日本一区二区免费高清| 99视频精品全部免费在线视频| 国产精品久久久久久妇女| 国产乱码精品一区二区亚洲| 噜噜噜久久亚洲精品国产品小说| av高清一区| 亚洲二区精品| 亚洲另类黄色| 精品视频在线一区二区在线| 亚洲精品**中文毛片| 欧美二区视频| 婷婷精品久久久久久久久久不卡| 国产一区白浆| 蜜桃av一区二区三区电影| 99成人在线视频| 一本一道久久a久久精品蜜桃| 日韩电影免费在线观看| 蜜臀av免费一区二区三区| 日韩激情综合| 成人小电影网站| 三级欧美在线一区| 妖精视频成人观看www| 偷拍亚洲精品| 人在线成免费视频| 首页亚洲欧美制服丝腿| 欧美激情视频一区二区三区在线播放| 91精品日本| 日韩大片免费观看| 综合激情在线| 日韩黄色大片| 日韩av资源网| 国产精品一区二区av日韩在线| 日韩中文字幕| 视频一区中文字幕| 九九九精品视频| 亚洲一区二区三区高清| 精品网站999| 在线精品福利| 国产精品成人a在线观看| 久久亚洲黄色| 国产精品va| 国产主播一区| 免费日韩一区二区三区| 亚洲在线免费| av免费不卡国产观看| 日韩毛片一区| 亚洲精品午夜av福利久久蜜桃| 香蕉国产精品| 一级欧洲+日本+国产| 国产另类在线| 国产在线视频欧美一区| 国产婷婷精品| 日韩高清成人| 国产精品2023| 视频一区欧美精品| 日韩欧美少妇| 久久精品系列| 青青草伊人久久| 日韩中文字幕一区二区三区| 另类中文字幕国产精品| 欧美国产另类| 欧美日韩伊人| 国产精品99久久精品| 日精品一区二区三区| 亚洲黄页一区| 久久国产中文字幕| 日韩深夜视频| 视频一区二区三区在线| 国产一区二区三区不卡视频网站| 久久中文字幕av| 久久久久亚洲精品中文字幕| 日韩高清一区二区| 爽好久久久欧美精品| 国产精品亚洲一区二区在线观看| 久久亚洲人体| 人人爱人人干婷婷丁香亚洲| а√天堂8资源中文在线| 奇米色欧美一区二区三区| 香蕉久久国产| 国产精品17p| 亚洲精品国产精品粉嫩| 亚洲欧美日韩精品一区二区| 蜜臀av免费一区二区三区| 91免费精品| 精品视频自拍| 亚洲综合丁香| 国产一区观看| 欧美 日韩 国产一区二区在线视频 | 日韩中文字幕一区二区高清99| 国产精品激情电影| 四虎精品一区二区免费| 日本大胆欧美人术艺术动态| 久久av中文| 日韩av中文在线观看| а√天堂8资源在线| 国产精品大片| 亚洲精品电影| 欧美.日韩.国产.一区.二区 | 日本国产一区| 婷婷综合成人| 91伊人久久| 国产精品777777在线播放| 国产精品最新自拍| 国产精品一区二区三区www| 国产精品亚洲产品| 国产精品香蕉| 夜久久久久久| 精品一区毛片| 欧美日韩国产高清| 亚洲欧美激情诱惑| 少妇精品在线| 国产精品一国产精品k频道56| 亚洲一区网站| 国产偷自视频区视频一区二区| 水蜜桃精品av一区二区| 亚洲色图综合| 日韩中文在线电影| 日韩精品五月天| 国产欧美激情| 三级在线观看一区二区| 蜜臀久久99精品久久久画质超高清 | 亚洲综合在线电影| 日本色综合中文字幕| 日韩高清不卡一区二区| 国产精品日本一区二区不卡视频 | 久久久久国产精品一区三寸| 日韩欧乱色一区二区三区在线| 九色porny丨国产首页在线| 青草综合视频| 国产精品九九| 日韩福利一区| 久久先锋影音| 国产欧美69| 日韩av免费| 亚洲欧美久久精品| 欧美激情五月| 激情欧美亚洲| 日韩av一级片| 蜜桃av在线播放| 爽好久久久欧美精品| 久久狠狠久久| 婷婷激情一区| 国产传媒在线观看| 欧美另类专区| 久久国产三级精品| 精品国模一区二区三区| 一区二区三区国产盗摄| 久久99精品久久久野外观看| 色综合www| 亚洲精品进入| 国产亚洲一区二区手机在线观看| 精品深夜福利视频| 久久久国产精品一区二区中文| 97精品视频在线看| 欧美成人久久| 日韩高清一区二区| 久久久噜噜噜| 日韩高清成人在线| 久久久国产亚洲精品| 日韩精品a在线观看91| 久久久成人网| 91成人福利| re久久精品视频| 国产毛片一区二区三区| 99视频精品全部免费在线视频| 精品亚洲美女网站| 婷婷综合成人| 99成人在线视频| 国产精品久av福利在线观看| 亚洲激情精品| 国内精品麻豆美女在线播放视频| а√天堂8资源在线| 视频一区二区欧美|