@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
}
}
/// 在项目中添加这么一段配置就可以对资源权限规则设定
默认
http.authorizeRequests((requests) -> requests.anyRequest().authenticated());
就是之前说的对所有请求都进行拦截了~
# 说明
- permitAll() 代表放行该资源,该资源为公共资源 无需认证和授权可以直接访问
- anyRequest().authenticated() 代表所有请求,必须认证之后才能访问
- formLogin() 代表开启表单认证
## 注意: 放行资源必须放在所有认证请求之前!
引入自定义登录界面~~ 我们可以先看看Springsecurity 为我们做的页面有什么因素
form 表单请求 请求方式是Post,
name="uname" 用户名 name="passwd" 密码
请求路径是/login
我们从刚刚追源码的代码上也可以看到这些
我们先做一个页面来替换他的默认页面呢??? 可以的
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
///我们加入一个 thymeleaf 的pom
我们的请求方式是Post,请求路径是doLogin,然后
用户名name的属性是uname
密码name的属性是passwd
Springboot 项目自然通过controller 来跳转到这个页面
/* http 用来去控制http 请求的*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.
authorizeRequests() // 代表开启请求权限管理
.mvcMatchers("/index").permitAll() // 代表匹配请求 针对/index 请求放行所有请求
//1. 放行login.html 的请求
.mvcMatchers("/login.html").permitAll()
.anyRequest()
.authenticated()
.and()
.formLogin()
/*替换的页面*/
.loginPage("/login.html") // 我们自定义的页面
/* 接受的请求url*/
.loginProcessingUrl("/doLogin")
/* 接受的请求的username*/
.usernameParameter("uname")
/* 接受的请求的password*/
.passwordParameter("passwd")
.successForwardUrl("/index")/*doLogin 内部跳转*/
// .defaultSuccessUrl("/index")/*index 302 请求转发*/
/*认证失败默认去/login.html 的url*/
.failureUrl("/login.html")
.and()
.csrf().disable()
;// 除了index 我们都需要做认证的 放行的 必须在第一个
}
现在就是说当我们 认证失败就会跳转到. .failureUrl("/login.html")
这个页面 重新认证
// .successForwardUrl("/index")/*doLogin 内部跳转*/
// .defaultSuccessUrl("/index")/*index 302 请求转发*/
这么2个看url 页面跳转的url 就可以看出来区别 一个是 内部跳转到/index 首页
一个是 请求转发 重定向到index
successForwardUrl 、defaultSuccessUrl 这两个方法都可以实现成功之后跳转
上面的
.successForwardUrl("/index")/*doLogin 内部跳转*/
// .defaultSuccessUrl("/index")/*index 302 请求转发*/
/*认证失败默认去/login.html 的url*/
.failureUrl("/login.html")
只是针对于传统web 开发的,如果成功之后跳转页面
如果失败之后也是跳转页面
但是 现在前后端开发的时候更多的是 成功后 返回给前端json数据
以及登录失败后返回给json 数据
@Override
protected void configure(HttpSecurity http) throws Exception {
http.
authorizeRequests() // 代表开启请求权限管理
.mvcMatchers("/index").permitAll() // 代表匹配请求 针对/index 请求放行所有请求
//1. 放行login.html 的请求
.mvcMatchers("/login.html").permitAll()
.anyRequest()
.authenticated()
.and()
.formLogin()
/*替换的页面*/
.loginPage("/login.html")
/* 接受的请求url*/
.loginProcessingUrl("/doLogin")
/* 接受的请求的username*/
.usernameParameter("uname")
/* 接受的请求的password*/
.passwordParameter("passwd")
/*成功之后跳转的路径*/
// .successForwardUrl("/index")/*doLogin 内部跳转*/
// .defaultSuccessUrl("/index")/*index 302 请求转发*/
/*认证失败默认去/login.html 的url*/
// .failureUrl("/login.html")
.successHandler(new AuthenticationSuccessHandler() {
// 自定义成功 认证 自定义认证成功之后的处理
/ 自定义一个成功的处理
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
//前后端分离就是响应一段json
Map<String, Object> result = new HashMap<>();
result.put("msg", "登录成功");
result.put("status", "200");
// 认证成功之后的数据都在authentication 中
result.put("authentication", authentication);
// 响应到前端
response.setContentType("application/json; charset=UTF-8");
String s = new ObjectMapper().writeValueAsString(result);
response.getWriter().println(s);
}
})
自定义失败的处理 前后端分离用
.failureHandler(new AuthenticationFailureHandler() {
/ 认证失败 回调 onAuthenticationFailure
//自定义认证失败的处理方案
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
Map<String, Object> result = new HashMap<>();
result.put("msg", "登录失败 :" + exception.getMessage());
// 认证失败
result.put("status", "400");
// 响应到前端
response.setContentType("application/json; charset=UTF-8");
String s = new ObjectMapper().writeValueAsString(result);
response.getWriter().println(s);
}
}) // 用来自定义
.and()
.csrf().disable()
;//
}