springsecurity搭建1
1.使用maven搭建项目矿建
2.引入依赖
引入spring-boot插件
4.编写启动类以及访问方法
@RestController
@SpringBootApplication
public class SecurityApplication {
@GetMapping("/")
public String hello(){
return "hello spring security";
}
public static void main(String[] args) {
SpringApplication.run(SecurityApplication.class, args);
}
}
通过启动类启动项目 启动成功后,访问页面(http://localhost:8080/) 会看到如下图一样的页面

用户名为user 密码是控制台中打印的一串字符串 登录后看到“hello spring security”
如果自定义用户名和密码 须在application.properties中添加如下的语句
spring.security.user.name=用户名
spring.security.user.password=密码
重新启动就可以使用了
默认表单认证
创建WebSecurityConfig类继承WebSecurityConfigureSAdapter.根据对WebSecurityConfigureSAdapter类中configure(HttpSecurity http)方法查看


默认声明了一些安全特性
1.验证所有请求(anyRequest()匹配请求)
2.formLogin()方法--允许用户使用表单登录身份验证
3.允许用户Http认证
有时候会遇到浏览器请求头中自动携带Authorization属性,导致无法看到登录页,是因为spring security的配置刚好同时支持HTTP基本认证,所以不需要在表单中重新登录,这是浏览器默认行为,如何避免这个问题尼,在IE浏览器中,可以在控制台执行(document.execCommand("ClearAuthenticationCache"))语句清楚HTTP基本认证缓存,但这个方法在Chrome中并不适用,建议调试时直接使用浏览器的无痕模式。这样就可以避免很多问题

浙公网安备 33010602011771号