概述
1 首先新建一个springboot项目,建立一个简单的Controller如下
@RestController
public class HelloController {
@GetMapping(value="/hello")
public String hello(){
return "hello";
}
}
此时访问将返回简单字符串如下
2 在pom.xml中引入SpringSecurity
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
3 此时再访问之间的hello接口会发现跳转到了登录页
也就是说此时springsecurity已经生效了,当访问接口是会弹出登录,其默认的用户名是user,密码在java启动的控制台中可以看到,如下,复制此密码,然后登录,发现可以看到输出hello
2022-03-25 11:52:48.553 INFO 22200 --- [ main] .s.s.UserDetailsServiceAutoConfiguration :
Using generated security password: 81419b78-39a2-47db-a59d-e5791a3141d3
2022-03-25 11:52:48.669 INFO 22200 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@153cd6bb, org.springframework.security.web.context.SecurityContextPersistenceFilter@3681037, org.springframework.security.web.header.HeaderWriterFilter@3ed03652, org.springframework.security.web.csrf.CsrfFilter@5833f5cd, org.springframework.security.web.authentication.logout.LogoutFilter@ad9e63e, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@285f38f6, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@46731692, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter@61d84e08, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@476e8796, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@ffaaaf0, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@228cea97, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@2d9f64c9, org.springframework.security.web.session.SessionManagementFilter@173797f0, org.springframework.security.web.access.ExceptionTranslationFilter@7a34f66a, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@6ef1a1b9]
2022-03-25 11:52:48.758 INFO 22200 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-03-25 11:52:48.761 INFO 22200 --- [ main] com.example.Application : Started Application in 3.738 seconds (JVM running for 4.444)
最后
以上就是欣喜悟空为你收集整理的Spring Security学习一:SpringSecurity入门的全部内容,希望文章能够帮你解决Spring Security学习一:SpringSecurity入门所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复