Spring Security - 引入依赖

时间 2021/5/28 14:31:06 加载中...

修改 pom.xml 文件

在准备的项目的 Pom.xml 文件中添加依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-security</artifactId>
  4. </dependency>
  5. <!--不是主要依赖:这个可以在 thymeleaf 中使用 spring security-->
  6. <dependency>
  7. <groupId>org.thymeleaf.extras</groupId>
  8. <artifactId>thymeleaf-extras-springsecurity5</artifactId>
  9. </dependency>

在 start.spring.io 创建项目的时候,也可以添加上 Spring Security 的依赖,也会自动
添加上 spring-boot-starter-security 和 thymeleaf-extras-springsecurity5 。

添加之后,会有一个默认的登录页面和用户,在控制台也会生成一个随机密码。
启动项目后,发现已经必须要登录了。我们在页面输入 user 和 控制台输出的密码就可以登录了。
这个的用户信息是默认保存在内存中的,而不是保存在数据库中。

控制台输出密码示例:

  1. 2021-06-01 16:13:21.771 WARN 7309 --- [ main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
  2. 2021-06-01 16:13:21.830 INFO 7309 --- [ main] .s.s.UserDetailsServiceAutoConfiguration :
  3. gl> Using generated security password: 7526d44d-a21f-4a30-bd4d-21999e055ceb
  4. 2021-06-01 16:13:21.939 INFO 7309 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Will secure any request with [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@47768e74, org.springframework.security.web.context.SecurityContextPersistenceFilter@76a805b7, org.springframework.security.web.header.HeaderWriterFilter@326e0b8e, org.springframework.security.web.csrf.CsrfFilter@3c8a7e38, org.springframework.security.web.authentication.logout.LogoutFilter@7ce85af2, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@118dcbbd, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@316acbb5, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter@2740e316, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@3c380bd8, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@167381c7, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@53ed80d3, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@5b5a4aed, org.springframework.security.web.session.SessionManagementFilter@41184371, org.springframework.security.web.access.ExceptionTranslationFilter@2f894ad9, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@6a9cd0f8]
  5. 2021-06-01 16:13:22.101 INFO 7309 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
扫码分享
版权说明
作者:SQBER
文章来源:http://www.sqber.com/articles/spring-security-add-dependency.html
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。