亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于Spring Boot保護(hù)Web應(yīng)用程序

 更新時(shí)間:2020年03月06日 09:34:48   作者:borter  
這篇文章主要介紹了基于Spring Boot保護(hù)Web應(yīng)用程序,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

如果在類路徑上添加了Spring Boot Security依賴項(xiàng),則Spring Boot應(yīng)用程序會(huì)自動(dòng)為所有HTTP端點(diǎn)提供基本身份驗(yàn)證。端點(diǎn)“/”和“/home”不需要任何身份驗(yàn)證。所有其他端點(diǎn)都需要身份驗(yàn)證。

要將Spring Boot Security添加到Spring Boot應(yīng)用程序,需要在構(gòu)建配置文件中添加Spring Boot Starter Security依賴項(xiàng)。

Maven用戶可以在pom.xml 文件中添加以下依賴項(xiàng)。

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

XML

Gradle用戶可以在build.gradle 文件中添加以下依賴項(xiàng)。

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

保護(hù)Web應(yīng)用程序

首先,使用Thymeleaf模板創(chuàng)建不安全的Web應(yīng)用程序。

然后,在 src/main/resources/templates 目錄下創(chuàng)建一個(gè)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>點(diǎn)擊 <a th:href = "@{/hello}">這里</a> 看到問候語.</p>
  </body>
</html>

HTML

使用Thymeleaf模板在HTML文件中定義的簡(jiǎn)單視圖/hello?,F(xiàn)在,在src/main/resources/templates目錄下創(chuàng)建一個(gè)文件: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

現(xiàn)在,需要為Home和hello視圖設(shè)置Spring MVC - View控制器。為此,創(chuàng)建一個(gè)擴(kuò)展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;

@Configuration
public 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

現(xiàn)在,將Spring Boot Starter安全依賴項(xiàng)添加到構(gòu)建配置文件中。Maven用戶可以在pom.xml 文件中添加以下依賴項(xiàng)。

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

XML

Gradle用戶可以在build.gradle 文件中添加以下依賴項(xiàng)。

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

現(xiàn)在,創(chuàng)建一個(gè)Web安全配置文件,該文件用于保護(hù)應(yīng)用程序以使用基本身份驗(yàn)證訪問HTTP端點(diǎn)。

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
@EnableWebSecurity
public 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

現(xiàn)在,在src/main/resources 目錄下創(chuàng)建一個(gè)login.html 文件,以允許用戶通過登錄屏幕訪問HTTP端點(diǎn)。

<!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}">
     你已經(jīng)注銷.
   </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 文件 - 允許用戶從應(yīng)用程序注銷并顯示當(dāng)前用戶名,如下所示 -

<!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應(yīng)用程序的代碼如下 -

package com.yiibai.websecuritydemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WebsecurityDemoApplication {
  public static void main(String[] args) {
   SpringApplication.run(WebsecurityDemoApplication.class, args);
  }
}

Java

下面給出了構(gòu)建配置文件的完整代碼。

Maven構(gòu)建文件 - pom.xml 的內(nèi)容如下:

<?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構(gòu)建文件 – 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.8

repositories {
  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‘)
}

現(xiàn)在,創(chuàng)建一個(gè)可執(zhí)行的JAR文件,并使用以下Maven或Gradle命令運(yùn)行Spring Boot應(yīng)用程序。

Maven用戶請(qǐng)使用下面給出的命令 -

mvn clean install

Shell

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

gradle clean build

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

現(xiàn)在,使用下面顯示的命令運(yùn)行JAR文件 -

java –jar <JARFILE>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring:@Async注解和AsyncResult與CompletableFuture使用問題

    Spring:@Async注解和AsyncResult與CompletableFuture使用問題

    這篇文章主要介紹了Spring:@Async注解和AsyncResult與CompletableFuture使用問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Spring?Bean獲取方式的實(shí)例化方式詳解

    Spring?Bean獲取方式的實(shí)例化方式詳解

    工作中需要對(duì)一個(gè)原本加載屬性文件的工具類修改成對(duì)數(shù)據(jù)庫的操作當(dāng)然,ado層已經(jīng)寫好,但是需要從Spring中獲取bean,然而,工具類并沒有交給Spring來管理,所以需要通過方法獲取所需要的bean。于是整理了Spring獲取bean的幾種方法
    2023-03-03
  • Java自然排序Comparable使用方法解析

    Java自然排序Comparable使用方法解析

    這篇文章主要介紹了Java自然排序Comparable使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Java多線程 自定義線程池詳情

    Java多線程 自定義線程池詳情

    這篇文章主要介紹了Java多線程 自定義線程池,文章主要是學(xué)習(xí)代碼,沒有過多解析,需要的朋友可以參考一下文章的具體內(nèi)容
    2021-10-10
  • Java使用Sa-Token框架完成踢人下線功能

    Java使用Sa-Token框架完成踢人下線功能

    踢人下線是一個(gè)很常見的需求,本文主要介紹了Java使用Sa-Token框架完成踢人下線功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • Spring security中的授權(quán)

    Spring security中的授權(quán)

    本篇為大家?guī)鞸pring security的授權(quán),首先要理解一些概念,有關(guān)于:權(quán)限、角色、安全上下文、訪問控制表達(dá)式、方法級(jí)安全性、訪問決策管理器,這篇文章主要介紹了Spring security中的授權(quán),需要的朋友可以參考下
    2024-01-01
  • java中JDBC實(shí)現(xiàn)往MySQL插入百萬級(jí)數(shù)據(jù)的實(shí)例代碼

    java中JDBC實(shí)現(xiàn)往MySQL插入百萬級(jí)數(shù)據(jù)的實(shí)例代碼

    這篇文章主要介紹了java中JDBC實(shí)現(xiàn)往MySQL插入百萬級(jí)數(shù)據(jù)的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-01-01
  • java使用sigar 遇到問題的快速解決方法

    java使用sigar 遇到問題的快速解決方法

    下面小編就為大家?guī)硪黄猨ava使用sigar 遇到問題的快速解決方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-06-06
  • Java util concurrent及基本線程原理簡(jiǎn)介

    Java util concurrent及基本線程原理簡(jiǎn)介

    這篇文章主要介紹了Java util concurrent及基本線程原理簡(jiǎn)介,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • SpringBoot?HikariCP配置項(xiàng)及源碼解析

    SpringBoot?HikariCP配置項(xiàng)及源碼解析

    這篇文章主要為大家介紹了SpringBoot?HikariCP配置項(xiàng)及源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02

最新評(píng)論