转载

Spring Boot2 入门 S1P01 Spring Security 使用

Spring Security,这是一种基于 Spring AOP 和 Servlet 过滤器的安全框架。它提供全面的安全性解决方案,同时在 Web 请求级和方法调用级处理身份确认和授权。

版本信息

Spring Boot: 2.1.8.RELEASE
Spring Security: 5.1.6.RELEASE

新建工程

工程依赖

dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation('org.springframework.boot:spring-boot-starter-security')
}

项目文件

SpringSecurity01Application.java

@SpringBootApplication
public class SpringSecurity01Application {
public static void main(String[] args) {
SpringApplication.run(SpringSecurity01Application.class, args);
}
}

controller/TestController.java

@RestController
public class TestController {
@GetMapping("test")
public String test() {
return "Spring Security!";
}
}

启动工程

启动日志

在启动日志里打印出了密码

......

Using generated security password: 9e262d11-0440-4328-9b50-e1d2091d853b

......
2019-09-24 21:25:05.834 INFO 13144 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-09-24 21:25:05.834 INFO 13144 --- [ main] c.i.s.SpringSecurity01Application : Started SpringSecurity01Application in 1.677 seconds (JVM running for 2.968)

访问接口

在浏览器中打开:

http://localhost:8080/test

弹出了登录页面:

Spring Boot2 入门 S1P01 Spring Security 使用

输入登录信息

// 用户名 user
username: user

// 控制台中打印的就是登录密码
password: 9e262d11-0440-4328-9b50-e1d2091d853b

这时就成功登录并打开了测试接口:

Spring Boot2 入门 S1P01 Spring Security 使用

修改登录信息

修改项目配置文件

application.properties

spring.security.user.name=admin
spring.security.user.password=123456

启动日志

在启动日志里没有打印密码

......
2019-09-24 21:40:22.982 INFO 5100 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-09-24 21:40:22.988 INFO 5100 --- [ main] c.i.s.SpringSecurity01Application : Started SpringSecurity01Application in 1.816 seconds (JVM running for 2.68)
2019-09-24 21:40:27.743 INFO 5100 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-09-24 21:40:27.744 INFO 5100 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-09-24 21:40:27.751 INFO 5100 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 7 ms
2019-09-24 21:40:28.080 WARN 5100 --- [nio-8080-exec-1] o.a.c.util.SessionIdGeneratorBase : Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [294] milliseconds.

访问接口

在浏览器中打开:

http://localhost:8080/test

弹出了登录页面:

Spring Boot2 入门 S1P01 Spring Security 使用

输入登录信息

这次输入的信息是在配置文件中配置的:

username: admin
password: 123456

成功登录并打开了测试接口:

Spring Boot2 入门 S1P01 Spring Security 使用

禁用安全设置

如果想要禁用安全设置

SpringSecurity01Application.java

@SpringBootApplication(exclude = {
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
})
public class SpringSecurity01Application {

public static void main(String[] args) {
SpringApplication.run(SpringSecurity01Application.class, args);
}

}

启动项目 & 访问接口

直接打开了测试接口:

Spring Boot2 入门 S1P01 Spring Security 使用

代码:

spring-security-01

参考资料:

spring-security

内容有误?联系作者:

Spring Boot2 入门 S1P01 Spring Security 使用

谢谢你请我吃糖果

Spring Boot2 入门 S1P01 Spring Security 使用 支付宝

Spring Boot2 入门 S1P01 Spring Security 使用 微信

原文  http://www.ihubin.com/blog/spring-boot-2-s1p01-spring-security/
正文到此结束
Loading...