springsecurity

spring security(一)——简单搭建spring s

2018-10-30  本文已影响0人  寻找大海的鱼

(一)pom.xml的依赖如下

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

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

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

        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity4</artifactId>
            <version>3.0.2.RELEASE</version>
        </dependency>

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


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

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

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

(二)application.properties

spring.session.store-type=none

spring.mvc.view.prefix=/
spring.mvc.view.suffix=.html

spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false

server.port=8080

(三)controller层

package com.wyh.spring_security.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: spring_security
 * @description:
 * @author: 一条懒咸鱼
 * @create:2018-30-18:42
 **/

@RestController
@RequestMapping("/")
public class UserController {

    @GetMapping
    public String hello(){
        return "hello,boy";
    }
}

(四)启动项目测试
由于我们没有自定义用户角色/密码,spring security默认用户为user,并会自动帮我们生成password,从控制台上我们可以看到spring security自动生成的password的值,如下图


key.png

在浏览器URL输入localhost:8080,便会进入用户验证界面,如下图


界面1.png
在User框里输入user
和在Password里输入spring security自动生成的password值
用户名和密码正确便进入我们写好的controller层
登陆成功.png
上一篇 下一篇

猜你喜欢

热点阅读