2013-07-18 40 views
2

大家好,我將在我的web應用程序中集成我寧靜的web服務的身份驗證。我已經將Spring安全3.1集成到了我的整個應用程序和它的工作中;但我很困惑,我真的陷入困境,如何在現有的應用程序中集成Web服務的安全性? 這裏是我現有的用於認證和授權的安全配置。爲Restful Web服務提供安全性,將其用於現有的Spring安全性3.1

securityApplicationContext.xml

<beans:bean id="myAccessDecisionManager" 
     class="com.security.repository.MyAccessDecisionManager"> 
     <beans:property name="customAuthenticatiuonService" ref="customAuthenticatiuonServiceImpl"> </beans:property> 
    </beans:bean> 

    <http auto-config="true" once-per-request="true" 
     access-decision-manager-ref="myAccessDecisionManager" access-denied-page="/jsp/errorPage.jsp"> 

     <intercept-url pattern="/*.web" access="ROLE_ANONYMOUS" /> 
<intercept-url pattern="productsService/*.web" access="ROLE_ADMIN" /> 

     <!-- Override default login and logout pages --> 
     <form-login login-page="/login.works" login-processing-url="/j_spring_security_check" 
      default-target-url="/login/validate.works" 
      authentication-failure-url="/login.works?login_error=1" /> 
     <logout logout-url="/j_spring_security_logout" 
      logout-success-url="/login.works" invalidate-session="true" /> 

     <session-management invalid-session-url="/login.works" 
      session-fixation-protection="newSession"> 
      <concurrency-control max-sessions="100" 
       error-if-maximum-exceeded="false" /> 
     </session-management> 
    </http> 

    <authentication-manager> 
     <authentication-provider ref="customAuthenticationProvider"></authentication-provider> 
    </authentication-manager> 

    <beans:bean id="customAuthenticationProvider" 
     class="com.security.repository.CustomAuthenticationProvider"> 
     <beans:property name="customAuthenticatiuonService" ref="customAuthenticatiuonServiceImpl"> </beans:property> 
    </beans:bean> 

    <beans:bean id="customAuthenticatiuonServiceImpl" 
     class="com.service.impl.CustomAuthenticatiuonServiceImpl"> 
     <beans:property name="customAuthenticationDAO" ref="customAuthenticationDAOTarget"> </beans:property> 
    </beans:bean> 

    <beans:bean id="customAuthenticationDAOTarget" class="com.dao.impl.CustomAuthenticatiuonDAOImpl"> 
     <beans:property name="hibernateTemplate" ref="cessHibernateTemplate"/> 
    </beans:bean> 

現在我期待它如下,以確保我的Web服務:

我的Web服務:

@Component 
@Path("/productsService") 
//@RequestMapping("/productsService") 
@Scope("request") 
@Controller 
public class ProductsController { 

    @Autowired 
    private ProductsService products; 

    @GET 
    @Path("/getProductsList.lbt") 
// @RequestMapping("/getProductsList.lbt") 
    @Produces("text/plain") 
    public String getProductsList() { 
     return products.getProductsList(); 
    } 
} 


@Service("products") 
public class ProductsService { 

    @Secured("ROLE_ADMIN") 
    public String getProductsList() {  
     return "Test String for Rest Web Service"; 
    } 
} 

和最後我的客戶類別:

public static void main(String[] args) { 
     Client c = Client.create(); 

    // plain text 
    WebResource r = c 
      .resource("http://localhost:8080/productsService/getProductsList.web"); 
    c.addFilter(new HTTPBasicAuthFilter("admin", "admin")); 
    c.setFollowRedirects(false); 
    System.out.println("Plain Text=>> " + r.get(String.class)); 

} 

我正在使用customAuthenticationManager和myAccessDecisionManger進行用戶的身份驗證和授權。 當我在控制器上使用@Path註解並對其進行調試時,調試器無法訪問我的控制器,並且發現402未找到錯誤,當我使用@RequestMapping時,它會正常運行,但是當從控制器返回時,我在客戶端302發現。 如何解決這個問題?請幫幫我。 在此先感謝。

回答

1

您應該使用兩個http標記。一個用於您的Web應用程序,另一個用於您的REST API。比方說,您可以爲您的Web應用程序使用入口點web/**,併爲您的REST API使用入口點API/**。您可能希望使用HTTP Basic來保護您的API,因此您的Web應用程序應該使用表單登錄(使用Java會話)和您的REST API使用HTTP基本身份驗證。 使用OAuth 2可以更好地保護REST API,但取決於您的應用程序的大小或受衆羣體將會過度。

+0

感謝您的回覆;現在我忙於另一項任務,一旦我有空,我會嘗試你的建議。 – Balasaheb

相關問題