【问题标题】:Spring Security access to a request, without pre-authentication, from remote accessSpring Security 从远程访问访问请求,无需预认证
【发布时间】:2012-10-11 11:35:36
【问题描述】:

我写在这里是因为我找不到我的问题的明确答案:

我的项目正在使用 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 security 网站上搜索过。他们谈论过滤器链、自己的用户名认证、RMI;但我并没有真正找到一个完整的例子来做我上面介绍的。

感谢任何能以这种方式帮助我的人。

ps:我使用所有默认或最简单的 Spring 安全配置(没有风水风格:-))

这是我的安全上下文 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>

还有web.xml文件中关于spring security的部分

<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>

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

【问题讨论】:

  • 将密码放在 URL 中是一个糟糕的主意,因为许多 Web 服务器会编写 URL 来访问日志。
  • 不是公网,是内网。
  • 尽管它在 Intranet 上并不安全,但有人可能站在已登录的人旁边并在 url 中看到他们的密码,但很多人在不同的系统上使用相同的密码,所以你真的让你的用户不安全。至少您应该将用户名和密码推送到 http 标头中。用户还会在即时通讯和电子邮件上剪切和粘贴网址,这可能会无意中导致问题。您是否希望某个使用您的应用程序的高级经理因为通过电子邮件发送链接并泄露了他的用户名/密码而生气

标签: java spring security getmethod


【解决方案1】:

假设您的 WEB-INF 中有一个applicationContext-security.xml
如果您可以发布您的 applicationContext-security.xml
,那就太好了 此外,您几乎拥有 Spring 安全性的默认配置。

我会推荐以下。

先把你的网址改成/public/get.request?request=getListCommand

然后在applicationContext-security.xml中添加如下sn-p

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

它将绕过/public/ 下的所有路径的安全性。 更改 URL 的原因是您可能不希望整个应用程序公开。

以下是我的 XML 示例。
还请注意,我的身份验证提供程序是自定义的,所以不要与此混淆。 您只需查看我的&lt;http&gt; 部分,您就会了解如何在您的应用程序中实现。

<?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/等),这意味着无需登录。

希望对您有所帮助。

如果您在理解方面需要进一步帮助,请告诉我。

【讨论】:

  • 我读了你说的,如果我理解正确的话,你向我解释了如何让用户在不登录的情况下访问我网站的某些部分。这不是完全错误,但我希望他们不是通过经典的 html 表单登录,而是直接在地址中给出他们的日志信息(这不是我的选择,我必须这样做)。我在上面添加了我的配置 xml 文件,以防万一
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-21
  • 2014-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-11
  • 1970-01-01
相关资源
最近更新 更多