2015-05-14 24 views
0

我正在使用java配置來應用spring安全性,並且我能夠在特定的url上應用安全性,但是我希望彈出安全性的默認登錄頁面,只要有人點擊除未受保護的url之外的其他URL。 這裏是我的SecurityConfig代碼:如何在特定的url上應用spring安全並使用java配置保留其他人?

import javax.sql.DataSource; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.http.HttpMethod; 
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 
//import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 
import org.springframework.security.config.annotation.web.builders.HttpSecurity; 
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 
import org.springframework.web.bind.annotation.RequestMethod; 

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter 
{ 

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

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .antMatchers("/myproject/userCont/user").permitAll() 
       .anyRequest().authenticated() 
       .and() 
       .formLogin() 
       .loginPage("/myproject/login/form") 
       .loginProcessingUrl("/login") 
       .failureUrl("/login/form?error") 
       .permitAll(); 
} 

所以,當我打/ myproject的/ userCont /用戶用GET方法是否能夠正常工作,但是當我打相同的URL與POST方法或其他網址春季安全不顯示默認登錄頁面。

任何人都可以幫助我嗎?

回答

0

doGet and doPost in Servlets

你應該通過上面的鏈接,以獲取有關GET和POST方法一個清晰的思路。

若要刪除/ myproject的/ userCont /用戶URL春季安全我們的代碼應該是這樣的:

@Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .anyRequest().authenticated() 
       .and() 
       .formLogin() 
       .loginPage("/myproject/login/form") 
       .loginProcessingUrl("/login") 
       .failureUrl("/login/form?error") 
       .permitAll(); 
} 

更進一步,你應該不是你的URL轉換成POST方法,因爲這將改變整個行爲都是你的網頁。

當我們在XML文件中

配置元素在裏面,你可以限制與 一個或多個元素訪問特定的網址。每個元素都指定一個URL模式 以及訪問這些URL所需的一組訪問屬性。請記住,您必須在網址格式結尾處始終包含通配符 。如果不這樣做,將使URL模式 無法匹配具有請求參數的URL。

<security:http auto-config="true" > 
<security:intercept-url pattern="/index*" access="ROLE_USER" /> 
<security:intercept-url pattern="/Transit*" access="ROLE_USER" /> 
<security:form-login login-page="/login.htm" default-target-url="/index.htm" 
    authentication-failure-url="/loginerror.htm" /> 
<security:logout logout-success-url="/logout.htm" /> 
</security:http> 

當過我們要描述一個網址,沒有任何保障,那麼我們應該從代碼下的安全配置XML文件,上面的行刪除特定的URL。例如,如果我們不需要任何索引頁面的安全性,那麼上面的代碼應該看起來像這樣。

<security:http auto-config="true" > 
    <security:intercept-url pattern="/Transit*" access="ROLE_USER" /> 
    <security:form-login login-page="/login.htm" default-target-url="/index.htm" 
     authentication-failure-url="/loginerror.htm" /> 
    <security:logout logout-success-url="/logout.htm" /> 
    </security:http> 
+0

@Qasim做ü認爲這個答案是適合你的問題 –

+0

感謝回答@MS易卜拉欣但是這不是我的問題,我想允許或禁止僅作爲我使用的彈簧引導通過Java配置URL和不想使用security.xml文件 – Qasim

相關問題