[英]Spring Security, secured and none secured access
我正在做一个需要先登录的小应用程序。 但是对于某些 3rd 方工具,我想提供一个不需要登录的 API。 登录本身工作正常,API 本身工作,但我不知道如何告诉 Spring Security,无需身份验证即可访问 API。 我在这里和其他网站上检查了几个主题并尝试了不同的版本,但没有一个有效。 每次我尝试访问 API 时,我都会被转发到登录表单并且必须先登录。
到目前为止,我的代码在我的 Spring Security 配置中看起来像这样:
/**
* configuration of spring security, defining access to the website
*
* @param http
* @throws Exception
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/rest/open**").permitAll()
.antMatchers("/login**").permitAll()
.and()
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/login")
.failureUrl("/login?error")
.defaultSuccessUrl("/dashboard")
.loginProcessingUrl("/j_spring_security_check")
.usernameParameter("username")
.passwordParameter("password")
.and()
.logout()
.logoutUrl("/j_spring_security_logout")
.logoutSuccessUrl("/login?logout")
.and()
.csrf();
}
还有我的控制器:
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PredictionOpenRestController {
@RequestMapping("/rest/open/prediction")
public String getPrediction() {
return "First Try!";
}
}
不知何故,我不得不感觉错过了什么。
请参阅Spring 安全参考:
我们的示例只要求对用户进行身份验证,并且对我们应用程序中的每个 URL 都进行了验证。 我们可以通过向
http.authorizeRequests()
方法添加多个子项来为我们的 URL 指定自定义要求。 例如:protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/resources/**", "/signup", "/about").permitAll() .antMatchers("/admin/**").hasRole("ADMIN") .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')") .anyRequest().authenticated() .and() // ... .formLogin(); }
1
http.authorizeRequests()
方法http.authorizeRequests()
每个匹配器都按照它们被声明的顺序来考虑。2 我们指定了任何用户都可以访问的多个 URL 模式。 具体来说,如果 URL 以“/resources/”开头、等于“/signup”或等于“/about”,则任何用户都可以访问请求。
3 任何以“/admin/”开头的 URL 将仅限于具有“ROLE_ADMIN”角色的用户。 您会注意到,由于我们正在调用 hasRole 方法,因此不需要指定“ROLE_”前缀。
4 任何以“/db/”开头的 URL 都要求用户同时拥有“ROLE_ADMIN”和“ROLE_DBA”。 您会注意到,由于我们使用了 hasRole 表达式,因此不需要指定“ROLE_”前缀。
5 任何尚未匹配的 URL 只需要对用户进行身份验证
您对.authorizeRequests()
第二次使用会覆盖第一次使用。
另见AntPathMatcher :
映射使用以下规则匹配 URL:
?
匹配一个字符
*
匹配零个或多个字符
**
匹配路径中的零个或多个目录例子
com/t?st.jsp
— 匹配com/test.jsp
但也匹配com/tast.jsp
或com/txst.jsp
com/*.jsp
— 匹配com
目录中的所有.jsp
文件
com/**/test.jsp
— 匹配com
路径下的所有test.jsp
文件
org/springframework/**/*.jsp
— 匹配org/springframework
路径下的所有.jsp
文件
org/**/servlet/bla.jsp
— 匹配org/springframework/servlet/bla.jsp
但也匹配org/springframework/servlet/bla.jsp
org/springframework/testing/servlet/bla.jsp
和org/servlet/bla.jsp
您修改后的代码:
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/rest/open/**").permitAll()
.antMatchers("/login/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.failureUrl("/login?error")
.defaultSuccessUrl("/dashboard")
.loginProcessingUrl("/j_spring_security_check")
.usernameParameter("username")
.passwordParameter("password")
.and()
.logout()
.logoutUrl("/j_spring_security_logout")
.logoutSuccessUrl("/login?logout")
.and()
.csrf();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.