SpringSecurity从入门到关门放狗(一)

SpringSecurity从入门到关门放狗(一)

———————集中式整合SpringBoot

你能看到这篇文章,说明你已经对spring security有了一定的了解。如果不了解也咩关系,知道这是个安全框架,它可以帮助你做登录校验(认证),角色权限控制(授权)就可以了。它和shiro的功能很像,但是它比shiro功能更加全面,相对的,它的使用也比shiro更复杂一些,使用哪种要看你们项目的复杂程度(公司大佬们的技术选型)等要素来决定。本文使用了SpringBoot整合的安全框架,搭建更加方便,废话不多说了,直接开淦。

如果对shiro有兴趣的同学,可以看看这篇springboot整合shiro和jwt-完整版,我见过的最完整的shiro实战文章!

入门Demo

1.先创建springboot项目,勾选web,spring security和dev-tools的依赖或者手动将代码添加到pom文件中
(1)勾选创建如下:
在这里插入图片描述
(2)pom文件中依赖和插件代码如下:

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

		<!-- 热部署 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<!-- test组件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

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

2.简单写一个首页页面放到static包中用于跳转使用(难看是难看了点,凑合用吧)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
<div>
    <h1>HELLO! SPRING SECURITY</h1>
</div>
</body>
</html>

3.创建一个controller类用于接收请求

@Controller
public class UserController{

    @RequestMapping("/toWelcome")
    public String toWelcome() {
        return "/";
    }
}

4.启动项目,使用浏览器访问localhost:8080/toWelcome路径
在这里插入图片描述

此时我们会看到一个登录页面,可是我们并没有写过这个登录页面啊,也不知道用户名和密码啊,这是怎么肥四呢?其实这是spring security框架默认的登录页面,现在spring security框架就已经发挥了功能,它将我们的请求拦截到了这个登录页面,只有我们进行登录(认证)操作之后才可以正常访问我们的资源。默认的用户名就是user,随机生成的密码被打印在了控制台。
在这里插入图片描述
当输入用户名和密码登录之后就可以访问到我们写的/toWelcome路径了,也就是跳转到index.html页面了
在这里插入图片描述

好了,入门demo到这里就结束了。
下一期我们会在现在的基础上加入我们自定义的登录和授权逻辑,各位不要着急哈~

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值