spring boot整合SpringSecurity 目录
spring boot整合SpringSecurity-01入门
spring boot整合SpringSecurity-02 基于Serssion的认证
spring boot整合SpringSecurity-03 自定义报错信息
spring boot整合SpringSecurity-04 使用jwt的方式认证
自定义报错信息
当我们没有配置报错信息,报错信息是这样的。
这样的报错信息并不是我们想要的。这个时候就需要我们自己定义报错信息。
配置文件信息
package com.hnbd.jinshui.config;
import com.hnbd.jinshui.security.handler.JWTAccessDeniedHandler;
import com.hnbd.jinshui.security.handler.JWTAuthenticationEntryPoint;
import com.hnbd.jinshui.security.handler.JwtLogoutSuccessHandler;
import com.hnbd.jinshui.service.UserDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
/**
* SecurityConfig 登录认证的配置
*
* @创建人 江枫沐雪
* @创建时间 2021/6/20 17:17
*/
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsServiceImpl userDetailsService;
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
//查询用户信息
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//用户详情信息
auth.userDetailsService(userDetailsService)
//密码加密方式
.passwordEncoder(bCryptPasswordEncoder);
}
//安全拦截机制
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
//配置跨域请求 并且 关闭打开的csrf保护
http .cors().and().csrf().disable()
// 认证配置
.authorizeRequests()
// 登录验证等放行
.antMatchers("/auth/**","/app/**").permitAll()
// 剩下的接口都需要登陆后访问
.anyRequest().authenticated()
.and()
//表单登录
.formLogin()
// 用户未登录的的时候跳转的这个路径
// .loginPage("/home")
// 用户登录时,用户名、密码提交的目的路径
.loginProcessingUrl("/login")
// 用户成功登录以后
.successForwardUrl("/success")
.and()
.logout()
.logoutUrl("/logout")
// 退出成功后返回
.logoutSuccessHandler(new JwtLogoutSuccessHandler())
.and()
// 异常处理
.exceptionHandling()
// 没有登录,返回
.authenticationEntryPoint(new JWTAuthenticationEntryPoint())
// 添加无权限时的处理
.accessDeniedHandler(new JWTAccessDeniedHandler());
}
}
退出时返回的数据。
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 退出成功处理器
* @创建人 江枫沐雪
* @创建时间 2021/8/1 15:00
*/
public class JwtLogoutSuccessHandler implements LogoutSuccessHandler {
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
// 返回结果 字符集编码
response.setCharacterEncoding("UTF-8");
// 设置内容类型
response.setContentType("application/json; charset=utf-8");
// response.getWriter().append(JSONConverter.toJSON(ApiResponseWithData.ofSuccess("成功推出")));
//返回的信息
response.getWriter().append("成功推出");
}
}
没有登录
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 没有登录
* @创建人 江枫沐雪
* @创建时间 2021/8/1 15:00
*/
public class JWTAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException, ServletException {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
// response.getWriter().write(JSONConverter.toJSON(ApiResponse.ofStatus(ApiResponseStatus.INVALID_TOKEN)));
response.getWriter().append("没有登录");
}
}
无权访问结果
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 无访问权限处理器
*
* @创建人 江枫沐雪
* @创建时间 2021/8/1 15:00
*/
public class JWTAccessDeniedHandler implements AccessDeniedHandler {
@Override
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
//设置字符编码
request.setCharacterEncoding("UTF-8");
//设置内容类型
response.setContentType("application/json; charset=utf-8");
response.getWriter().append("没有权限");
// httpServletResponse.getWriter()
// .write(JSONConverter.toJSON(ApiResponse.ofStatus(ApiResponseStatus.NO_PERMISSION)));
}
}