Springboot+Security用户无权限返回自定义信息

@Springboot+Security用户无权限返回自定义信息

返回信息分为跳转自定义页面和字符串提醒

返回信息分为跳转自定义页面和字符串提醒,这里我将都介绍并将自己遇到的问题做出解释

返回自定义界面提示

  1. 只需要修改启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.example.App.security.UserService;

@SpringBootApplication
@ComponentScan(basePackages =“com.example.App”)
@EnableAutoConfiguration
public class LoginApplication extends WebMvcConfigurerAdapter {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	SpringApplication.run(LoginApplication.class, args);
}

 @Bean
    public UserDetailsService userDetailsService() {
        return new UserService();
    }
    /**
     * 自定义异常页
     */
 @Bean
 public EmbeddedServletContainerCustomizer containerCustomizer() {

    return (container -> {
         //可以定义多个自定义页面
         //注意:如果使用thymeleaf错误页面默认src/main/resources/templates/error路径下
         //否则在main/resources/static路径下
         //具体关于页面路径问题请详细阅读https://blog.csdn.net/w995223851/article/details/88350054
         ErrorPage error403Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/403.html");
         ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
         //参数可变
         container.addErrorPages(error403Page,error404Page);
    });
 }
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
    }

}

返回自定义消息提示

  1. 新建MyAccessDeniedHandler类实现AccessDeniedHandler接口

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

/*

  • 无权限用户提示设置
    */
    @Component
    public class MyAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response,AccessDeniedException accessDeniedException) throws IOException, ServletException {

     //返回json形式的错误信息        
     response.setCharacterEncoding("UTF-8");
     response.setContentType("application/json");
     response.getWriter().println("我愚蠢的弟弟啊 ! 你跑错厕所了 ! ");
     response.getWriter().flush();
    

    }

}

2.在 继承了WebSecurityConfigurerAdapter 的WebSecurityConfig类中添加和修改

添加
@Bean
public AccessDeniedHandler getAccessDeniedHandler() {
return new MyAccessDeniedHandler();
}
修改 configure(HttpSecurity http)方法
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
//无权限用户提示字符串消息设置
.exceptionHandling()
// getAccessDeniedHandler()是上文的方法
.accessDeniedHandler(getAccessDeniedHandler())
//and()是连接不同配置的
.and()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值