继续接着上篇文章 《Spring Security(四):自定义用户认证逻辑》,本篇文章主要介绍如何进行个性化用户认证。
场景:考虑到前后端分离的场景,后端提供接口供前端调用,并返回JSON格式的数据给前端。或请求html请求返回页面,来处理不同类型的请求(html请求或数据请求)。
处理不同类型的请求
1、【zjj-security-browser 工程】修改安全框架的配置类BrowserSecurityConfig
package com.zjj.security.browser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import com.zjj.security.core.properties.SecurityProperties;
@Configuration
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private SecurityProperties securityProperties;
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin() // 定义是否使用表单登录
// .loginPage("/zjj-log