Rad*_*ski 3 javascript spring spring-mvc spring-security angularjs
我通过将 Authorization 标头添加到 $http 标头来遵循Spring Security 和 Angular JS进行 HTTP 基本身份验证:
const headers = {
authorization: "Basic " + btoa(this.login + ":" + this.password)
};
this._$http.get("user",
{
headers: headers,
})
.then(
this.showUser.bind(this),
this.showError.bind(this)
);
Run Code Online (Sandbox Code Playgroud)
在里面showUser我重定向到jobs组件$location:
this._$location.path("jobs");
Run Code Online (Sandbox Code Playgroud)
在jobs组件内部,我加载可用的作业:
public $onInit() {
this._$http.get("jobs").then(function(response) {
this.jobs = response.data;
}.bind(this));
this.authenticated = this._loginService.isLogged();
}
Run Code Online (Sandbox Code Playgroud)
故意未经授权标头,以证明一切正常。我认为它应该被Spring SecuritywithHTTP 401 Unauthorized或类似的东西放弃,但它在没有授权标头的情况下工作。当我从另一个浏览器窗口注销并重新加载作业时,一切正常,作业未加载。但我认为(并且我希望)授权数据(HTTP Basic)应该出现在每个请求中。这是我的安全配置:
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
.successHandler(
new DifferentRoleBasedStartingURLsAuthenticationSuccessHandler()
)
.and()
.logout()
.logoutUrl("/logout")
.and()
.httpBasic()
.and()
.authorizeRequests()
.antMatchers("/jobs/**").authenticated()
.antMatchers("/interviews/**").authenticated()
.anyRequest().permitAll()
.and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
;
Run Code Online (Sandbox Code Playgroud)
也许在这里我犯了一个错误。我认为规则也.antMatchers("/jobs/**").authenticated()应该经过jobs/验证。你能帮助我吗?先感谢您。
2016-07-31 更新:也许 Spring 中的 Angular 请求不需要授权标头?我的仓库在这里:Playground for Spring and Angular (AngularJS 分支),密码适用test于每个创建的用户。
如果您使用基本身份验证,那么在 spring 中配置表单登录没有多大意义。使用基本身份验证时,需要在服务器上进行身份验证的每个请求都需要存在 http 授权标头。如果受保护资源不存在授权标头,Spring 将返回 401 响应。
你的设置对我来说看起来是正确的(是的,“/jobs/**”与“/jobs”匹配),但我猜你验证安全约束是否有效的实验失败了,因为服务器还设置了一个可以使用的 jsessionid cookie来验证您的身份。因此,如果您请求没有授权标头但具有有效 jsessionid cookie 的受保护资源,则服务器仍然可以对您进行身份验证。
您可以通过将会话创建策略设置为 STATELESS 来告诉 spring-security 不要与 http 会话交互。
尝试以下安全配置:
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic()
.and()
.authorizeRequests()
.antMatchers("/jobs/**").authenticated()
.antMatchers("/interviews/**").authenticated()
.anyRequest().permitAll()
.and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1756 次 |
| 最近记录: |