2015-09-16 43 views
2

我有一個由Tomcat提供的Java應用程序(即http://111.222.333.444:8080/myApp)。我的客戶希望這個應用程序響應一個友好的域名(即http://client.example.com)。我不是很好用Apache或Tomcat,但我配置了Apache這樣做重定向:Apache重定向到Tomcat的安全應用程序

<VirtualHost 111.222.333.444:80> 
    ServerName client.example.com 
    ProxyRequests Off 
    ProxyPreserveHost On 
    <Proxy *> 
     Order deny,allow 
     Allow from all 
    </Proxy> 
    ProxyPass/http://111.222.333.444:8080/myApp/ 
    ProxyPassReverse/http://111.222.333.444:8080/myApp/ 
    ProxyPassReverseCookieDomain 111.222.333.444 client.example.com 

    ProxyPass /login.html http://111.222.333.444:8080/login.html 
    ProxyPassReverse /login.html http://111.222.333.444:8080/login.html 
</VirtualHost> 

這個偉大的工程http://client.example.com下就可以提供應用程序。不過,我有一個使用Spring Security實現的登錄頁面。從http://111.222.333.444:8080/myApp/login.html訪問它時,登錄工作正常。但是,當我從http://client.example.com/login.html訪問它並輸入有效的用戶名和密碼時,它只是再次加載登錄頁面而沒有錯誤。

My WebSecurityConfigurerAdapter看起來像這樣。我無法正確加載登錄頁面,而無法將絕對URL傳遞給loginPage()。

@Configuration 
@EnableWebMvcSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    public void configure(WebSecurity web) throws Exception { 
     web.ignoring().antMatchers("/resources/**"); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.csrf().disable(); 
     http.authorizeRequests() 
       .antMatchers("/admin/**") 
       .hasRole("ADMIN") 
       .and() 
       .formLogin() 
       .loginPage("http://client.example.com/login.html") 
       .and() 
       .logout() 
       .logoutSuccessUrl("/"); 
    } 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     auth.inMemoryAuthentication().withUser("admin").password("admin").roles("ADMIN"); 
    } 
} 

我的登錄頁面,如下所示:

<form name="f" method="post" action="/login.html">    
    <fieldset> 
     <legend>Please Login</legend>   
     <label for="username">User</label> 
     <select id="username" name="username"> 
      <option value="-1"></option> 
      <option value="admin">admin</option> 
     </select>   
     <label for="password">Password</label> 
     <input type="password" id="password" name="password"/> 
     <div> 
      <button type="submit" role="button"><span>Login</span></button> 
     </div> 
    </fieldset> 
</form> 

所以,當我訪問http://client.example.com/admin/test.html,我正確地重定向到http://client.example.com/login.html。但是當我嘗試使用「admin」/「admin」登錄時,它只是再次登錄登錄頁面http://client.example.com/login.html而沒有任何錯誤。 apache日誌,tomcat日誌和應用程序日誌不會告訴我任何有用的信息。

我認爲它與Apache代理不能很好地與Spring Security配合使用,因爲它在直接點擊Tomcat應用程序時工作,但我不確定。

  1. 我該如何獲得Apache/Spring Security來正確處理登錄?我希望應用程序能夠識別出我已經登錄並將我引導到我試圖訪問的管理頁面。
  2. 有沒有辦法讓登錄頁面在WebSecurityConfigurerAdapter中不使用絕對URL的情況下工作?

Apache的版本:阿帕奇/ 2.2.31(Unix的)
Spring Security的版本:3.2.6.RELEASE
Tomcat的版本:的Apache Tomcat/7.0.42
Java版本:Java的1.7

回答

0

我覺得你的登錄表單的行動不應該被指向登錄頁面的URL,而是

/j_spring_security_check 

的UsernamePasswordAuthenticationFilter正在尋找一種以模仿AU認證過程。如果你不希望使用/ j_spring_security_check,你可以告訴UsernamePasswordAuthenticationFilter通過設置呼叫loginProcessingUrl方法對你FormLoginConfigurer這樣去尋找另一個網址:

formlogin() 
    .loginProcessingUrl('/custom_login_url') 
    .loginPage("http://client.domain.com/login.html") 
+0

謝謝你的建議。我之前實際上已經嘗試過,沒有運氣。當我將表單動作更改爲/ j_spring_security_check時,表單發佈到htt p://client.domain.com/j_spring_security_check,並且我得到了「405不允許的方法」。我也嘗試將字段更改爲j_username和j_password,因爲這就是它看起來像UsernamePasswordAuthenticationFilter正在尋找,但沒有幫助。 – byrdley20

+0

當您將表單發佈操作設置爲「/ j_spring_security_check」時,您是否禁用了csrf? – fractonimbus

+0

是的,它被禁用。我曾經試圖啓用它,它成功地發送CSRF令牌中的POST,但後來我得到一個「403禁止」。做我必須爲了正確使用Spring Security配置HTTPS? – byrdley20

相關問題