我读了很多关于如何配置 Spring Security 的手册,但仍然坚持配置。 所以我想配置休息调用和其他http调用。据我了解,我可以创建像/server/** 这样的 URL - 用于 Web 应用程序和/rest/** - 用于其余应用程序。对于任何 Web 应用程序 URL 的调用,我想创建一个登录页面(当用户未经身份验证时),但对于其余应用程序,我想触发未经授权的代码。
我通过扩展 WebSecurityConfigurerAdapter 使用 spring 注释来实现
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests()
.antMatchers("/").access("hasRole('ROLE_USER')")
.antMatchers("/server/*").access("hasRole('ROLE_USER')")
.and()
.formLogin().loginPage("/login").permitAll().failureUrl("/login?error")
.usernameParameter("username")
.passwordParameter("password")
.and()
.exceptionHandling()
.accessDeniedPage("/accessDenied");
}
对于服务器,它工作正常,但如果我尝试在此处添加/rest,当我尝试调用/rest/[something] (在浏览器中)时,它总是将我转发到/login 页面。 我不明白为什么,这让我心碎。 感谢您提供任何有用的回复。
最佳答案
你有这个:
.antMatchers("/").access("hasRole('ROLE_USER')")
.antMatchers("/server/*").access("hasRole('ROLE_USER')")
.and()
.formLogin()
意味着/访问需要ROLE_ACCESS并且进行身份验证,直接到formLogin
您需要多个身份验证配置。
参见http://docs.spring.io/autorepo/docs/spring-security/4.0.0.CI-SNAPSHOT/reference/htmlsingle/
其中一个你需要这样的东西
http
.antMatcher("/rest/**")
.exceptionHandling().authenticationEntryPoint(new AccessDenyEntryPoint()).and()
.authorizeRequests().antMatchers("/spring/**").denyAll();
关于java - Spring 保护 Web 应用程序和 Rest 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28974038/