2012-10-11 66 views
1

我寫到這裏,因爲我無法找到一個明確的答案,我的問題:春季安全訪問的請求,不預認證,從遠程訪問

我的項目是使用Spring MVC和Spring Security的。我很好地安裝了一個Web應用程序(當然使用Java)。我可以通過post和get方法訪問,但只能在用戶通過Spring Security的常用形式連接後才能訪問。

從目前來看,用戶在這樣的一個地址,一個請求:

../../get.request?request=getListCommand

其中get.request是Spring MVC的一個映射。只有在用戶通過身份驗證後才能啓用此訪問權限!

我需要做的:添加的可能性可以直接訪問這個請求,沒有以前已認證,使用的地址是這樣一個例子:

http://123.123.123.123:123/get.request?request=getListCommand&j_password=myPassword&j_username=myName

same thing with the post protocol and the params given (request=getListCommand, j_password=myPassword, j_username=myName)

當然,認證必須在先前完成請求並將結果發回。

我在很多網站或直接在Spring安全網站上搜索過。他們談論過濾鏈接,自己的用戶名認證,RMI;但我並沒有真正找到一個完整的例子來完成我上面介紹的內容。

謝謝任何​​人都可以幫助我。

PS:我使用所有默認或春季安全(無風水」風格:-))

這裏是我的SECURIT背景下的XML文件

<http realm="NexCap Up" 
     auto-config="true" 
     access-denied-page="/www/jsp/authentication/accessDenied.jsp" 
     create-session="always" 
     disable-url-rewriting="true">   
      <port-mappings> 
       <port-mapping http="8084" https="8443"/> 
      </port-mappings>   

      <intercept-url pattern="/www/jsp/authentication/connexion.jsp"      
       access='IS_AUTHENTICATED_ANONYMOUSLY' requires-channel="https"/> 

      <intercept-url pattern="/www/jsp/authentication/connexionFailed.jsp" 
       access='IS_AUTHENTICATED_ANONYMOUSLY' /> 

      <intercept-url pattern="/www/jsp/authentication/applicationExit.jsp" 
       access='IS_AUTHENTICATED_ANONYMOUSLY' /> 


      <intercept-url 
       pattern="/get.Request" 
       method="GET" 
       access="ROLE_REMOTE" /> 

      <intercept-url 
       pattern="/post.Request" 
       method="POST" 
       access="ROLE_REMOTE" /> 

      <intercept-url pattern="/**" 
       access="ROLE_REMOTE,ROLE_SCRIPT" /> 
     <form-login 
      authentication-failure-url="/www/jsp/authentication/connexionFailed.jsp" 
      login-page="/www/jsp/authentication/connexion.jsp" 
      default-target-url="/www/jsp/index.jsp" 
      always-use-default-target="true"/> 

     <logout 
      logout-success-url="/www/jsp/authentication/applicationExit.jsp" 
      invalidate-session="true"/> 

     <session-management 
      invalid-session-url="/www/jsp/authentication/invalidSession.jsp" 
      session-authentication-error-url = "/www/jsp/authentication/authentificationError.jsp" 
      session-fixation-protection="none"> 

      <!-- Sessions concurrentes --> 
      <concurrency-control 
       error-if-maximum-exceeded="false" 
       expired-url="/www/jsp/authentication/sessionExpired.jsp" 
       max-sessions="1" /> 

     </session-management> 

    </http> 

最簡單的配置和的部分關於春天的安全

<security-constraint> 
    <web-resource-collection> 
     <web-resource-name>Security</web-resource-name> 
     <url-pattern>/*</url-pattern> 
    </web-resource-collection> 
    <user-data-constraint> 
     <transport-guarantee>CONFIDENTIAL</transport-guarantee> 
    </user-data-constraint> 
</security-constraint> 


<filter> 
    <display-name>springSecurityFilterChain</display-name> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class> 
     org.springframework.web.filter.DelegatingFilterProxy 
    </filter-class> 

<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
web.xml文件

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     /WEB-INF/spring/secu-config.xml 
    </param-value> 

+0

將您的密碼放在URL中是一個可怕的想法,因爲許多Web服務器將寫URLS來訪問日誌。 – ams

+0

這不是公共網絡,而是內部網。 – user954156

+0

即使它在Intranet上不安全,某人可能站在登錄人員的旁邊,並在url中查看密碼,但很多人對不同的系統使用相同的密碼,所以您確實使用戶不安全。至少應該將用戶名和密碼推入http標題中。用戶也會通過即時消息和電子郵件剪切和粘貼網址,這可能會不經意間造成問題。你想讓一些高級經理使用你的應用程序變得生氣,因爲他通過電子郵件發送了一個鏈接,並且泄露了他的用戶名/密碼 – ams

回答

0

假設有在你的WEB-INF一個applicationContext-security.xml
如果您可以發佈您的applicationContext-security.xml
而且您幾乎具有spring安全性的默認配置,那將是非常好的選擇。

我會推薦以下。

首先將網址變更爲/public/get.request?request=getListCommand

然後下面的代碼片段添加到的applicationContext-security.xml文件

<http use-expressions="true"> 
<intercept-url pattern="/public/**" access="permitAll" /> 
<!-- You can add n number of filters here--> 
</http> 

它的作用是爲/public/下的所有路徑繞過安全。 更改URL的原因是您可能不希望整個應用程序公開。

下面是我的XML示例。
另請注意,我的身份驗證提供程序是自定義的,所以不要與此混淆。 所有你需要的是看我的<http>部分,你會知道如何在你的應用程序中實現。

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/security" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
         http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd"> 

    <global-method-security pre-post-annotations="enabled" jsr250-annotations="enabled" secured-annotations="enabled"> 
     <!-- AspectJ pointcut expression that locates our "post" method and applies security that way 
     <protect-pointcut expression="execution(* bigbank.*Service.post*(..))" access="ROLE_TELLER"/> 
     --> 
    </global-method-security> 

    <http use-expressions="true"> 
     <intercept-url pattern="/favicon.ico" access="permitAll" /> 
     <intercept-url pattern="/static/**" access="permitAll"/> 
     <intercept-url pattern="/login.jsp*" access="permitAll"/> 
     <intercept-url pattern="/Admin/**" access="hasAnyRole('ROLE_SUPER_USER')"/> 
     <form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?login_error=1" /> 
     <http-basic/> 
     <logout logout-success-url="/login.jsp"/> 
     <remember-me user-service-ref="loginService" /> 
     <!-- Uncomment to limit the number of sessions a user can have --> 
    </http> 

    <authentication-manager> 
     <authentication-provider user-service-ref="loginService"> 
     <password-encoder hash="md5"/> 
     </authentication-provider> 
    </authentication-manager> 

    <beans:bean id="loginService" class="com.indyaah.service.LoginService"> 
    </beans:bean> 
    <beans:bean id="authService" class="com.indyaah.service.AuthService" /> 
</beans:beans> 

正如你可以看到我讓login.jsp和我的靜態內容(圖片/ JS/CSS /等)要訪問匿名意味着無需登錄。

希望這有助於。

讓我知道你是否需要進一步幫助理解。

+0

我讀了你說的話,如果我理解了,你向我解釋瞭如何讓用戶訪問我網絡的某些部分網站沒有登錄。這並不是完全錯誤,但我希望它們不是通過傳統的html表單登錄,而是直接在地址中給出它們的日誌信息(這不是我的選擇,我必須這樣做)。我在上面添加了我的config xml文件,以防有所幫助 – user954156