电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

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

基于Spring Boot保護Web應用程序

瀏覽:10日期: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.hdgsjgj.cn/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
相關文章:
主站蜘蛛池模板: 水篦子|雨篦子|镀锌格栅雨水篦子|不锈钢排水篦子|地下车库水箅子—安平县云航丝网制品厂 | 干洗店加盟_洗衣店加盟_干洗店设备-伊蔻干洗「武汉总部」 | 行星齿轮减速机,减速机厂家,山东减速机-淄博兴江机械制造 | 四川成人高考_四川成考报名网| 底部填充胶_电子封装胶_芯片封装胶_芯片底部填充胶厂家-东莞汉思新材料 | 品牌广告服务平台,好排名,好流量,好生意。 | 多米诺-多米诺世界纪录团队-多米诺世界-多米诺团队培训-多米诺公关活动-多米诺创意广告-多米诺大型表演-多米诺专业赛事 | 合肥角钢_合肥槽钢_安徽镀锌管厂家-昆瑟商贸有限公司 | 深圳市人通智能科技有限公司| 佛山商标注册_商标注册代理|专利注册申请_商标注册公司_鸿邦知识产权 | 【黄页88网】-B2B电子商务平台,b2b平台免费发布信息网 | 恒温槽_恒温水槽_恒温水浴槽-上海方瑞仪器有限公司 | 谈股票-今日股票行情走势分析-牛股推荐排行榜 | 砖机托板价格|免烧砖托板|空心砖托板厂家_山东宏升砖机托板厂 | 双菱电缆-广州电缆厂_广州电缆厂有限公司 | 智能垃圾箱|垃圾房|垃圾分类亭|垃圾分类箱专业生产厂家定做-宿迁市传宇环保设备有限公司 | 大_小鼠elisa试剂盒-植物_人Elisa试剂盒-PCR荧光定量试剂盒-上海一研生物科技有限公司 | 全自动烧卖机厂家_饺子机_烧麦机价格_小笼汤包机_宁波江北阜欣食品机械有限公司 | 附着力促进剂-尼龙处理剂-PP处理剂-金属附着力处理剂-东莞市炅盛塑胶科技有限公司 | 安徽泰科检测科技有限公司【官方网站】 | 橡胶接头_橡胶软接头_套管伸缩器_管道伸缩器厂家-巩义市远大供水材料有限公司 | 商用绞肉机-熟肉切片机-冻肉切丁机-猪肉开条机 - 广州市正盈机械设备有限公司 | 横河变送器-横河压力变送器-EJA变送器-EJA压力变送器-「泉蕴仪表」 | 钢板仓,大型钢板仓,钢板库,大型钢板库,粉煤灰钢板仓,螺旋钢板仓,螺旋卷板仓,骨料钢板仓 | 直流电能表-充电桩电能表-导轨式电能表-智能电能表-浙江科为电气有限公司 | 金属清洗剂,防锈油,切削液,磨削液-青岛朗力防锈材料有限公司 | 好看的韩国漫画_韩漫在线免费阅读-汗汗漫画 | 免费个人pos机申请办理-移动pos机刷卡-聚合收款码办理 | TwistDx恒温扩增-RAA等温-Jackson抗体-默瑞(上海)生物科技有限公司 | 二次元影像仪|二次元测量仪|拉力机|全自动影像测量仪厂家_苏州牧象仪器 | 欧盟ce检测认证_reach检测报告_第三方检测中心-深圳市威腾检验技术有限公司 | 专业广州网站建设,微信小程序开发,一物一码和NFC应用开发、物联网、外贸商城、定制系统和APP开发【致茂网络】 | 生物颗粒燃烧机-生物质燃烧机-热风炉-生物颗粒蒸汽发生器-丽水市久凯能源设备有限公司 | 安徽集装箱厂-合肥国彩钢结构板房工程有限公司 | 药品/药物稳定性试验考察箱-埃里森仪器设备(上海)有限公司 | 压片机_高速_单冲_双层_花篮式_多功能旋转压片机-上海天九压片机厂家 | 郑州宣传片拍摄-TVC广告片拍摄-微电影短视频制作-河南优柿文化传媒有限公司 | 沈阳液压泵_沈阳液压阀_沈阳液压站-沈阳海德太科液压设备有限公司 | 帽子厂家_帽子工厂_帽子定做_义乌帽厂_帽厂_制帽厂_帽子厂_浙江高普制帽厂 | 十字轴_十字轴万向节_十字轴总成-南京万传机械有限公司 | 德州网站开发定制-小程序开发制作-APP软件开发-「两山开发」 |