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
弹出了登录页面:
// 用户名 user
username: user
// 控制台中打印的就是登录密码
password: 9e262d11-0440-4328-9b50-e1d2091d853b
这时就成功登录并打开了测试接口:
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
弹出了登录页面:
这次输入的信息是在配置文件中配置的:
username: admin
password: 123456
成功登录并打开了测试接口:
如果想要禁用安全设置
@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-security-01参考资料:
spring-security
内容有误?联系作者:
谢谢你请我吃糖果
支付宝
微信