2016-02-15 55 views
0

我有一個項目,我有一個應用程序「a」與其餘服務和另一個(「b」)的網頁,用戶可以登錄和使用1或2個「a」的休息服務(例如發表評論)。
問題是,其餘服務都是安全的,並應通過httpBasic每次(無會話)進行授權。
另一方面,「b」所需的服務應有一個會話。
這樣:
請求直接與其他服務 - > httpBasic每次(無會話)通過網頁/網頁處理請求
登錄 - >通過登錄會話春季安全 - 多個會話取決於使用(登錄/ httpBasic)

有沒有辦法來實現這一目標?我現在唯一的解決方案是在httpBasic進程後刪除會話,但我不知道該怎麼做。

httpSecurity.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED) 
    .and() 
    .authorizeRequests().anyRequest().permitAll() 
    .and().httpBasic(); 
+0

我不明白這個問題。有沒有辦法做什麼?您希望網頁在每次訪問其餘服務時登錄? –

+0

對不起,它更像這樣: 我想直接通過客戶端調用我的webservices,那些應該總是通過httpBasic頭授權 - 不會以這種方式創建會話。 但我也有一個網頁,在發佈消息,評論等(webservices)之前,需要登錄(獲取會話)。這些應通過會議授權。 – user3272488

+0

同樣的事情不是嗎?網頁需要將授權標題存儲在會話中並每次發送。 –

回答

0

如果你真的要刪除它可以通過實現LogoutSuccessHandler接口來實現會話數據。

 package com.arjun.security; 

     import java.io.IOException; 

     import javax.servlet.ServletException; 
     import javax.servlet.http.HttpServletRequest; 
     import javax.servlet.http.HttpServletResponse; 

     import org.springframework.beans.factory.annotation.Autowired; 
     import org.springframework.security.core.Authentication; 
     import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; 
     import org.springframework.stereotype.Service; 

     import com.informage.arnav.domain.CassandraLoginSession; 

     @Service 
     public class AppLogoutSuccessHandler implements LogoutSuccessHandler { 

      @Autowired 
      private CassandraLoginSessionDao cassandraLoginSessionDao; 

      public AppLogoutSuccessHandler() { 
       super(); 
      } 

      @Override 
      public void onLogoutSuccess(HttpServletRequest request, 
             HttpServletResponse response, 
             Authentication authentication) throws IOException, ServletException { 

       if (authentication != null) { 
        final Object principal = authentication.getPrincipal(); 
        final AppUserDetails appUserDetails; 
        if (principal != null && principal instanceof AppUserDetails) { 
         appUserDetails = (AppUserDetails) principal; 
       CassandraLoginSession cassandraLoginSession=cassandraLoginSessionDao.findBySessionId(appUserDetails.getSessionId().toString()); 
      //can also delete based on userId     
      //CassandraLoginSession cassandraLoginSession=cassandraLoginSessionDao.findByUserId(appUserDetails.getUserId()); 
         cassandraLoginSessionDao.hardDelete(cassandraLoginSession); 
      //delete the session data from DB where session is stored 
        } 
       } 
      } 

     } 

在您的安全配置中,您需要配置此處理程序。

<logout logout-url="/logout" success-handler-ref="appLogoutSuccessHandler" /> 
0

你可以寫下面以允許你的Web服務訪問,而不登錄:

http.authorizeRequests()  
    .antMatchers("/webserviceNotRequiredAuthorization*").permitAll(); 

您必須把下面給你web服務需要授權:

http.authorizeRequests().antMatchers("/webserviceRequiredAuthorization").hasAnyRole("ROLE_ADMIN"); 

總之先寫不需要授權的URL模式,然後編寫需要授權的URL模式。

http.authorizeRequests()  
    .antMatchers("/webserviceNotRequiredAuthorization*").permitAll();  

http.authorizeRequests().antMatchers("/webserviceRequiredAuthorization").hasAnyRole("ROLE_ADMIN");