活动地址:CSDN21天学习挑战赛
目前,在 Java web 开发中,安全框架常用的一般有 SpringSecurity 和 Shiro:
SpringSecurity 特点:
Spring 无缝整合web 开发而设计
web 环境使用core 模块和 web 模块。单独引入 core 模块Shiro 特点:
Shiro 主张的理念是把复杂的事情变简单。针对对性能有更高要求的互联网应用有更好的表现web 环境,可以脱离 web 环境使用web 环境下一些特定的需求需要手动编写代码定制基于我们开发中绝大部分情况下都是使用
Spring框架,且对性能要求没有很高,所以我们一般选择SpringSecurity框架,毕竟Spring 出品,必属精品。
SpringSecurity 是一个安全框架,前身是 Acegi Security,能够为 Spring 企业应用系统提供声明式的安全访问控制。Spring Security 基于 Servlet 过滤器、IoC 和 AOP,为 Web 请求和方法调用提供身份验证和授权处理,避免了代码耦合,减少了大量重复代码工作。
SpringBoot 项目,并且提供一个 hello 接口:@RestController
public class HelloController {
@GetMapping("hello")
public String hello() {
return "hello spring security";
}
}
启动项目后,访问接口:

SpringBoot 为我们提供了集成 SpringSecurity 的 starter:这里使用
SpringBoot2.3.12.RELEASE版本,对应Security5.3.9.RELEASE版本
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-securityartifactId>
dependency>
并且 SpringBoot 已经为 SpringSecurity 提供了默认配置,默认所有资源(包括静态资源)都必须认证通过才能访问。此时再访问 hello 接口,会跳转默认的登录页面:

这里 SpringBoot 为我们提供了默认的用户名:user,密码为随机生成在控制台:

输入正确的用户名密码,认证通过后就可以继续访问了:

可能第一次使用 SpringSecurity 的朋友们都会有下面这些疑惑:
在之后的 SpringSecurity 学习 专栏中,我们一起来揭开 Spring Security 神秘的面纱。