通过java代码配置弹簧安全性的自定义403错误页面
任何人都知道如何在spring security中配置自定义的403页面? 在Web上看,我得到的所有结果都是XML配置,我正在使用Java配置。 那是我的SecurityConfig:
@Configuration @ComponentScan(value="com") @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) public class SecurityConfig extends WebSecurityConfigurerAdapter { @Bean @Override public AuthenticationManager authenticationManagerBean() throws Exception { return new CustomAuthenticationManager(); } protected void configure(HttpSecurity http) throws Exception { http .csrf() .disable() .authorizeRequests() .antMatchers("/resources/**", "/publico/**").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/acesso/login").permitAll() .loginProcessingUrl("/login").permitAll() .usernameParameter("login") .passwordParameter("senha") .successHandler(new CustomAuthenticationSuccessHandler()) .failureHandler(new CustomAuthenticationFailureHandler()) .and() .logout() .logoutUrl("/logout") .logoutSuccessUrl("/acesso/login").permitAll(); } }
我也有AccessDeniedHandler的自定义实现:
public class CustomAccessDeniedHandler implements AccessDeniedHandler { @Override public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException arg2) throws IOException, ServletException { response.sendRedirect(request.getContextPath() + "/erro/no_permit"); } }
如果我是对的,为了个性化页面403,您可以使用此服务器实现的模型。
Spring Security:自定义403拒绝访问页面
例:
AppConfig.java
@Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/resources/**", "/signup").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .permitAll() .and() .exceptionHandling().accessDeniedPage("/403") .and() .logout().logoutUrl("/logout").logoutSuccessUrl("/") .and() .rememberMe() .and() .csrf().disable(); }
HomeController.java
@RequestMapping("/403") public String accessDenied() { return "errors/403"; }
而.html,将是一个自定义页面,其中包含一些消息403。
- 使用smack API发送和接收消息
- new运算符和Class.newInstance()有什么区别?
- 使用SpringData创建只读存储库
- 来自bouncycastle RSAKeyParameters的Publickey
- REST API,关于403错误代码?
- 严重:配置类org.apache.catalina.deploy.ApplicationListener的应用程序侦听器时出错
- 停止Spring计划执行,如果它在一段固定时间后挂起
- 当使用JtaTransactionManager时,为什么使用未在Spring服务中提交的EntityManager进行JPA更改?
- 使用相机将基本渲染3D透视投影到2D屏幕上(无opengl)