Geeks_Z の Blog Geeks_Z の Blog
首页
  • 学习笔记

    • 《HTML》
    • 《CSS》
    • 《JavaWeb》
    • 《Vue》
  • 后端文章

    • Linux
    • Maven
    • 汇编语言
    • 软件工程
    • 计算机网络概述
    • Conda
    • Pip
    • Shell
    • SSH
    • Mac快捷键
    • Zotero
  • 学习笔记

    • 《数据结构与算法》
    • 《算法设计与分析》
    • 《Spring》
    • 《SpringMVC》
    • 《SpringBoot》
    • 《SpringCloud》
    • 《Nginx》
  • 深度学习文章
  • 学习笔记

    • 《PyTorch》
    • 《ReinforementLearning》
    • 《MetaLearning》
  • 学习笔记

    • 《高等数学》
    • 《线性代数》
    • 《概率论与数理统计》
  • 增量学习
  • 哈希学习
GitHub (opens new window)

Geeks_Z

AI小学生
首页
  • 学习笔记

    • 《HTML》
    • 《CSS》
    • 《JavaWeb》
    • 《Vue》
  • 后端文章

    • Linux
    • Maven
    • 汇编语言
    • 软件工程
    • 计算机网络概述
    • Conda
    • Pip
    • Shell
    • SSH
    • Mac快捷键
    • Zotero
  • 学习笔记

    • 《数据结构与算法》
    • 《算法设计与分析》
    • 《Spring》
    • 《SpringMVC》
    • 《SpringBoot》
    • 《SpringCloud》
    • 《Nginx》
  • 深度学习文章
  • 学习笔记

    • 《PyTorch》
    • 《ReinforementLearning》
    • 《MetaLearning》
  • 学习笔记

    • 《高等数学》
    • 《线性代数》
    • 《概率论与数理统计》
  • 增量学习
  • 哈希学习
GitHub (opens new window)
  • Linux

  • 数据结构与算法

  • 算法设计与分析

  • Java

  • Python

  • 设计模式

  • 计算机网络

  • Spring笔记

  • SpringMVC笔记

  • SpringBoot笔记

  • SpringSecurity

    • SpringSecurity
    • SpringSecurity简介
    • SpringSecurity Web 权限方案
    • 新建 Markdown
    • 新建 Markdown
    • Handler
      • 实现 AuthenticationSuccessHandler 接口
      • SecurityConfig 修改配置
      • 实现 AuthenticationFailureHandler 接口
      • SecurityConfig 修改配置
    • 注解使用
    • CSRF
    • 访问控制
  • Elasticsearch笔记

  • RabbitMQ笔记

  • Docker笔记

  • MySQL

  • Redis

  • Mybatis

  • MybatisPlus

  • Nginx

  • Kubernetes笔记

  • Git

  • Software

  • 微服务笔记

  • bug

  • BackEndNotes
  • SpringSecurity
Geeks_Z
2023-09-04
目录

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

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

自定义登录失败处理器

实现 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

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
上次更新: 2024/03/29, 08:56:31
新建 Markdown
注解使用

← 新建 Markdown 注解使用→

最近更新
01
并行训练
03-29
02
tensor维度转换
03-26
03
ResNet源码解读
03-23
更多文章>
Theme by Vdoing | Copyright © 2022-2024 Geeks_Z | MIT License
京公网安备 11010802040735号 | 京ICP备2022029989号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式