【发布时间】:2016-12-27 09:45:39
【问题描述】:
我正在做一个需要先登录的小应用程序。但是对于一些 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!";
}
}
不知何故,我不得不感觉错过了一些东西。
【问题讨论】:
标签: java spring security spring-security