这篇文章将为大家详细讲解有关一文教你正确的使用springsecurity,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
springsecurity是基础springboot的,所以创建一个springboot工程引入依赖就可以很轻松的整合springsecurity了。(类似的权限管理框架还有shiro)
1. 创建一个普通的springboot项目(不用勾选任何东西),我这边使用的springboot版本是2.2.1.RELEASE
依赖如下:pom.xml
<dependencies>
<!--spring web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--spring security依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
随意编写一个测试的controller即可,eg:TestController.java
package com.sixteen.springsecurity01.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping("/hello")
public String hello(){
return "hello security";
}
}
启动springboot服务,打开控制台会发现有这么一串东西
Using generated security password: 649a23c2-fcbc-4f9b-b643-0a0d8167dcf4
当你在浏览器输入上面的controller时,会弹出一个登录的界面:如图
出现这个界面就说明springsecurity整合进来了,springsecurity默认有一个用户名为user
,密码就是控制台那一串649a23c2-fcbc-4f9b-b643-0a0d8167dcf4
,输入之后点击login in就可以访问到controller了
关于一文教你正确的使用springsecurity就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。