SpringSecurity学习日记(3):常用方法

it2025-02-28  24

自定义类继承WebSecurityConfigurerAdapter类可以实现自己的用户授权验证逻辑。我们需要在 protected void configure(HttpSecurity http)方法中实现

@Override protected void configure(HttpSecurity http) throws Exception { // 对请求进行验证 http.authorizeRequests() // 以下请求可以直接访问,不需要认证 .antMatchers("/login", "/regist").permitAll() // 其他请求都需要认证 .anyRequest().authenticated() .and() // 定义登录页面与请求的URL .formLogin().loginPage("/login.html").loginProcessingUrl("/login") // 定义登录成功后重定向的地址 .successForwardUrl("/index") // 定义登录失败后重定向的地址 .failureForwardUrl("/failer") // 定义登录成功与登录失败的请求不需要授权 .permitAll() .and() // 定义注销的URL请求与注销成功后的重定向的地址 .logout().logoutUrl("/logout").logoutSuccessUrl("/login") // 注销用户后清除session .invalidateHttpSession(true) // 定义注销请求不需要授权 .permitAll() .and() // 关闭csrf认证 .csrf().disable(); }
最新回复(0)