我有要求在JBoss 6.4.0服務器上運行Spring Boot(version 1.3.0.RELEASE)應用程序。在JBoss上運行Spring Boot應用程序 - 忽略端點的問題
最初,遇到了以下問題,並根據作者的建議添加了解決方案。
http://ilya-murzinov.github.io/articles/spring-boot-jboss/
我仍然但是遇到問題。該應用程序使用Spring安全來管理訪問,並且它已被配置爲忽略某些路徑。不幸的是,當在JBoss上運行時,看起來設置爲忽略的端點沒有被拾取,並且嘗試登錄失敗(以及所有其他被忽略的端點)。
下面是代碼示例,顯示瞭如何忽略終點。也許這些被錯誤地實現,或者排序是一個問題?
package com.company.product.security;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
/**
* The configuration class responsible for handling security
*/
@Configuration
@EnableWebSecurity
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Value("${server.servlet-path}")
private String basePath;
@Autowired
private JWTAuthenticationProvider authenticationProvider;
@Autowired
private TokenHandler tokenHandler;
/**
* Adding custom provider to global authentication manager
*
* @param auth the authentication manager
*/
@Autowired
public void configureGlobal(final AuthenticationManagerBuilder auth) {
auth.authenticationProvider(this.authenticationProvider);
}
@Override
public void configure(final WebSecurity web) throws Exception {
//Specifies unsecured end-points
//previous approach example
//web.ignoring().antMatchers(this.basePath + "/login")
web.ignoring().antMatchers(this.basePath + "/login/**") //end point still cannot be reached
.antMatchers(this.basePath + "/endpoint1/**")
.antMatchers(this.basePath + "/endpoint2/**")
.antMatchers(this.basePath + "/v2/endpoint3/**");
}
@Override
protected void configure(final HttpSecurity http) throws Exception {
http.addFilterBefore(new StatelessAuthenticationFilter(this.tokenHandler),
UsernamePasswordAuthenticationFilter.class).authorizeRequests().anyRequest().authenticated().and()
.csrf().disable();
}
}
該代碼使用其嵌入式Tomcat服務器正常工作。
當我們嘗試訪問登錄端點時,我們正在獲取拒絕訪問錯誤。此端點不應具有任何安全性,我們已將其作爲忽略模式添加到我們的配置中。對於靜態頁面(例如html),忽略配置似乎工作正常,但在這種情況下不會。
檢查'basePath'屬性是否正確填充,是否與您期望的匹配。 –
Thanks @ M.Deinum是的,basePath具有期望的值。 – Gordon