spring-boot安全框架简单小例子

spring security 安全框架

前言

spring security 为网站提供安全

有两个功能: 认证(Authentication)我即我 和授权(Authorization)

同类框架 Shiro,

原始的认证和授权方式,使用拦截器实现认证和授权

hello小案例

目录结构
setting.gradle
build.gradle
src/main/java/test/Run.java
src/main/java/test/TestController.java
src/main/java/resources/application.yml
代码

setting.gradle

rootProject.name = 'demo'
build.gradle
plugins {
	id 'org.springframework.boot' version '2.2.7.RELEASE'
	id 'io.spring.dependency-management' version '1.0.9.RELEASE'
	id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
 
repositories {
	maven { url 'https://maven.aliyun.com/repository/public/' }
	maven { url 'https://maven.aliyun.com/repository/spring/'}
	mavenLocal()
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-security'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	testImplementation('org.springframework.boot:spring-boot-starter-test') {
		exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
	}
	testImplementation 'org.springframework.security:spring-security-test'
}

test {
	useJUnitPlatform()
}

添加重点依赖

implementation 'org.springframework.boot:spring-boot-starter-security'
入口类RUN.java
package test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RUN {
    public static void main(String[] args) {
        SpringApplication.run(RUN.class,args);
    }
}

控制类TestController.java
package test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
    @GetMapping("/")
    @ResponseBody
    public String index(){ return "成功登录";}
}
基本操作

1.运行程序,并查看运行日志,注意Using generated security password:这个后面的密码

gradle spring-boot:run

在这里插入图片描述

2.打开浏览器输出下面网站,出现默认的登录界面

http://127.0.0.1:8080/login

在这里插入图片描述

3.在浏览器输入账号,日志的密码,出现下面的结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值