美文网首页Java
Spring Security之Hello World

Spring Security之Hello World

作者: TZX_0710 | 来源:发表于2019-09-17 10:33 被阅读0次

Spring Security介绍
Spring Security是一个功能强大且可高度自定义的身份验证和访问控制框架。它是保护基于Spring的应用程序的框架。
Spring Security是一个专注于为Java应用程序提供身份验证和授权的框架。与所有Spring项目一样,Spring Security的真正强大之处在于它可以轻松扩展以满足自定义要求

  • springboot版本 2.1.8 接下来的演示都采用的最新的SpringBoot的版本
  1. 导入pom文件 引入springsecurity的依赖
<?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.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring_security</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring_security</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>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

配置application.yml

server:
  port: 8080
spring:
  freemarker:
    enabled: true
    cache: false
    template-loader-path: classpath:/templates/
    suffix: .html
  security:
    user:
      name: user
      password: user
      roles: user,admin

编写 hello.html login.html hello页面为展示页面 login页面为 登录界面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>helloworld</title>
</head>
<body>
        第一个Security应用
</body>
</html>

编写helloworld控制层

@Controller
public class HelloWorld {

    @RequestMapping(value = "/hello")
    public String hello(){
        return "hello";
    }
}

在这个时候我们可以在地址栏输入http://localhost:8080/hello
页面跳转到了security的默认配置页

image.png
输入yml文件配置的用户名和密码
image.png

相关文章

网友评论

    本文标题:Spring Security之Hello World

    本文链接:https://www.haomeiwen.com/subject/mijquctx.html