Handler
自定义登录成功处理器
实现 AuthenticationSuccessHandler
接口
/**
* 这里只是一个简单的示范,
* 还可以使用response给前端返回json数据
*/
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
private String url;
public MyAuthenticationSuccessHandler(String url){
this.url = url;
}
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
response.sendRedirect(url);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SecurityConfig 修改配置
/**
* 1.创建一个配置类继承自WebSecurityConfigurerAdapter
* 2.重写configure(AuthenticationManagerBuilder auth)方法, 设置用户名和密码
* 3.添加PasswordEncoder组件, 可能是用来解码的吧
*/
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
// 注意: 在设置了登录表单的url和可以直接访问的url后, 如果不进一步调用permitAll()方法, 则只是将这些url设置进去了,
// 但是还是需要权限才能访问, 而想要访问就需要登录, 但是访问登录页面页面需要权限, 就会造成尴尬的情况。
// 因此在设置了某些登录表单的url必须进一步调用permitAll()方法。
// permitAll(): 使用permitAll()将配置授权,以便在该特定路径上允许所有请求
@Override
protected void configure(HttpSecurity http) throws Exception {
// 登录表单的设置
http.formLogin()
.loginPage("/login.html") // 配置登录页的url
.loginProcessingUrl("/user/login") // 设置提交登录的url, 我们不需要编写这个url对应的控制器
// .defaultSuccessUrl("/index") // 登录成功的跳转路径, redirect方式; successForwardUrl()方法为forword方式
// 设置自定义登录成功的处理器
.successHandler(new MyAuthenticationSuccessHandler("<http://www.baidu.com>"))
.failureUrl("/hello") // 登录失败的跳转路径, redirect方式; failureForwardUrl()方法为forword方式
.permitAll(); // 将这些路径授予访问权限
// 设置可以直接访问的请求
http.authorizeRequests()
.antMatchers("/", "/hello", "/user/login") // 表示请求路径
.permitAll(); // 将这些路径授予访问权限
// 设置其余请求都需要进行登录
http.authorizeRequests()
.anyRequest() // 其他请求
.authenticated(); // 需要认证
// 关闭csrf防护, 可以先理解为防火墙
http.csrf().disable();
}
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
自定义登录失败处理器
实现 AuthenticationFailureHandler
接口
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
private String url;
public MyAuthenticationFailureHandler(String url){
this.url = url;
}
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
response.sendRedirect(url);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
SecurityConfig 修改配置
/**
* 1.创建一个配置类继承自WebSecurityConfigurerAdapter
* 2.重写configure(AuthenticationManagerBuilder auth)方法, 设置用户名和密码
* 3.添加PasswordEncoder组件, 可能是用来解码的吧
*/
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
// 注意: 在设置了登录表单的url和可以直接访问的url后, 如果不进一步调用permitAll()方法, 则只是将这些url设置进去了,
// 但是还是需要权限才能访问, 而想要访问就需要登录, 但是访问登录页面页面需要权限, 就会造成尴尬的情况。
// 因此在设置了某些登录表单的url必须进一步调用permitAll()方法。
// permitAll(): 使用permitAll()将配置授权,以便在该特定路径上允许所有请求
@Override
protected void configure(HttpSecurity http) throws Exception {
// 登录表单的设置
http.formLogin()
.loginPage("/login.html") // 配置登录页的url
.loginProcessingUrl("/user/login") // 设置提交登录的url, 我们不需要编写这个url对应的控制器
// .defaultSuccessUrl("/index") // 登录成功的跳转路径, redirect方式; successForwardUrl()方法为forword方式
// 设置自定义登录成功的处理器
.successHandler(new MyAuthenticationSuccessHandler("<http://www.baidu.com>"))
// 设置自定义登录失败的处理器
.failureHandler(new MyAuthenticationFailureHandler("/hello"))
.failureUrl("/hello") // 登录失败的跳转路径, redirect方式; failureForwardUrl()方法为forword方式
.permitAll(); // 将这些路径授予访问权限
// 设置可以直接访问的请求
http.authorizeRequests()
.antMatchers("/", "/hello", "/user/login") // 表示请求路径
.permitAll(); // 将这些路径授予访问权限
// 设置其余请求都需要进行登录
http.authorizeRequests()
.anyRequest() // 其他请求
.authenticated(); // 需要认证
// 关闭csrf防护, 可以先理解为防火墙
http.csrf().disable();
}
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
上次更新: 2024/03/29, 08:56:31