0
点赞
收藏
分享

微信扫一扫

【Spring Security】入门案例



文章目录

  • ​​入门案例​​
  • ​​1.基本流程​​
  • ​​2.步骤实施​​

入门案例

1.基本流程

【Spring Security】入门案例_maven


​​返回顶部​​

2.步骤实施

创建SpringBoot项目:


【Spring Security】入门案例_maven_02

【Spring Security】入门案例_Spring Security_03

【Spring Security】入门案例_maven_04

【Spring Security】入门案例_xml_05

【Spring Security】入门案例_maven_06

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.zyx</groupId>
<artifactId>securitydemo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>securitydemo1</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- security依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

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

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>

修改配置 — 默认端口改为8111:


【Spring Security】入门案例_maven_07

创建完成项目后,编写简单的测试接口 /test/hello。


【Spring Security】入门案例_Spring Security_08

启动项目后访问:localhost:8111/test/hello,会自动跳转到一个登陆页面,这其实就是security的作用,只有进行了验证授权后才能正常访问。


【Spring Security】入门案例_maven_09

细心的伙伴会发现,在启动项目的时候会多出一行:Using generated security password: 8bf98367-8f8a-4cc8-a464-8778fa0ff304,这就是每次启动的时候默认给出的登陆密码,登陆用户名默认的是​user​

【Spring Security】入门案例_xml_10

​​返回顶部​​


举报

相关推荐

0 条评论