Spring 保护网络应用和休息应用
Spring secure both web app and rest app
我阅读了很多关于如何配置 spring 安全性的手册,但仍然坚持配置。
所以我想配置休息电话和其他 http 电话。据我所知,我可以创建像 /server/** 这样的 url - 用于 web 应用程序和 /rest/** - 用于 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/[something](在浏览器中)时尝试在此处添加 /rest,它总是将我转发到 /login 页面。
我不明白为什么,这让我心碎。
感谢您提供任何有用的回复。
如果您不想验证其余网址,则必须再添加一个 .antMatchers("/rest/*").permitAll()
。
你有这个:
.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();
我阅读了很多关于如何配置 spring 安全性的手册,但仍然坚持配置。 所以我想配置休息电话和其他 http 电话。据我所知,我可以创建像 /server/** 这样的 url - 用于 web 应用程序和 /rest/** - 用于 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/[something](在浏览器中)时尝试在此处添加 /rest,它总是将我转发到 /login 页面。 我不明白为什么,这让我心碎。 感谢您提供任何有用的回复。
如果您不想验证其余网址,则必须再添加一个 .antMatchers("/rest/*").permitAll()
。
你有这个:
.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();