Spring Security学习一:SpringSecurity入门

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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值