Java Spring安全配置httpsecurity
我第一次尝试Spring安全性,但确实遇到了这样一个问题:我有一个默认的网页,应该是默认的非身份验证网页,我有一批控制器调用,我想用一个预授权的注释来保护它。基本思想是,我想禁用默认的“重定向到登录页面”,但仍然有机会操作Spring Security的方法安全复合体 我使用的是java配置,它看起来像:Java Spring安全配置httpsecurity,java,spring,security,spring-mvc,spring-security,Java,Spring,Security,Spring Mvc,Spring Security,我第一次尝试Spring安全性,但确实遇到了这样一个问题:我有一个默认的网页,应该是默认的非身份验证网页,我有一批控制器调用,我想用一个预授权的注释来保护它。基本思想是,我想禁用默认的“重定向到登录页面”,但仍然有机会操作Spring Security的方法安全复合体 我使用的是java配置,它看起来像: @Configuration @EnableWebSecurity public class SpringWebSecurityConfig extends WebSecurityConfig
@Configuration
@EnableWebSecurity
public class SpringWebSecurityConfig extends WebSecurityConfigurerAdapter{
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/res/**"); // #3
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**").authorizeRequests().anyRequest().permitAll();
}
}
我知道(或似乎理解)到目前为止,我所有的电话都应该被允许(在过去的两天里,我一直在考虑这个问题,显然没有什么想法了)
我想保护控制器的方法是:
@PreAuthorize("hasRole('ADMIN')")
@RequestMapping(value="/admin", method = RequestMethod.GET)
public String getAdminPage(Model model){
return "admin";
}
我知道我可以使用antMatcher添加“/**/admin”并授权对特定url的调用,但总体思路是:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**").authorizeRequests().anyRequest().permitAll();
@EnableGlobalMethodSecurity(prePostEnabled = true)
http.exceptionHandling().authenticationEntryPoint(new AuthenticationEntryPoint() {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException, ServletException {
if (authException != null) {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.getWriter().print("Unauthorizated....");
}
}
});