最近开始学习了springsecurity框架,为写后台页面做个权限管理什么的打基础。
springsecurity是基础springboot的,所以创建一个springboot工程引入依赖就可以很轻松的整合springsecurity了。(类似的权限管理框架还有shiro)
1. 创建一个普通的springboot项目(不用勾选任何东西),我这边使用的springboot版本是2.2.1.RELEASE
依赖如下:
pom.xml
org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-securitymysql mysql-connector-javacom.baomidou mybatis-plus-boot-starter3.0.5 org.projectlombok lombokorg.springframework.boot spring-boot-starter-testtest org.junit.vintage junit-vintage-engine
随意编写一个测试的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整合好了。
到此这篇关于springsecurity 基本使用的文章就介绍到这了,更多相关springsecurity使用内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!



