Java Spring安全访问来自远程访问的请求,无需预验证

Java Spring安全访问来自远程访问的请求,无需预验证,java,spring,security,getmethod,Java,Spring,Security,Getmethod,我写这封信是因为我找不到问题的明确答案: 我的项目使用SpringMVC和SpringSecurity。我为一个web应用程序(当然是使用Java)很好地安装了这两者。我可以使用post和get方法进行访问,但只有在用户通过常用的Spring安全性连接之后 从现在起,用户对如下地址执行请求: 。/../get.request?request=getListCommand 其中get.request是来自SpringMVC的映射。只有在用户经过身份验证后,才能启用此访问 我需要做什么:添加直接访问

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

我的项目使用SpringMVC和SpringSecurity。我为一个web应用程序(当然是使用Java)很好地安装了这两者。我可以使用post和get方法进行访问,但只有在用户通过常用的Spring安全性连接之后

从现在起,用户对如下地址执行请求:

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

其中get.request是来自SpringMVC的映射。只有在用户经过身份验证后,才能启用此访问

我需要做什么:添加直接访问此请求的可能性,而无需事先进行身份验证,使用类似以下地址,例如:

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

与post协议和给定的参数(request=getListCommand,j_password=myPassword,j_username=myName)相同。

当然,身份验证必须在执行请求和返回结果之前完成

我在许多网站上搜索,或者直接在Spring security网站上搜索。他们谈论过滤链,自己的用户名认证,RMI;但我并没有找到一个完整的例子来做我上面介绍的事情

谢谢任何能帮助我的人

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

这是我的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>

以及web.xml文件中关于spring安全性的部分

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

安全
/*
保密的
springSecurityFilterChain
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy


springSecurityFilterChain
/*


上下文配置位置
/WEB-INF/spring/secu-config.xml

假设您的WEB-INF中有一个
applicationContext security.xml

如果您可以发布applicationContext security.xml,那就太好了
而且您几乎有spring安全性的默认配置

我建议如下

首先将url更改为
/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>
如您所见,我允许匿名访问
login.jsp
和我的静态内容(images/js/css/etc),这意味着无需登录

希望这有帮助


如果您需要进一步了解,请告诉我。

在URL中输入密码是个糟糕的主意,因为许多web服务器会将URL写入访问日志。它不是公共网络,但是对于intranet来说,即使它是在intranet上的,它也不安全。有人可能站在登录的人旁边,并在url中看到他们的密码,但许多人在不同的系统中使用相同的密码,因此您确实使您的用户不安全。至少您应该将用户名和密码推送到http头中。此外,用户还会在im和电子邮件上剪切和粘贴URL,这可能会在不经意间造成问题。你想让某位使用你的应用程序的高级经理因为他发了一个链接并泄露了用户名/密码而生气吗?我读了你的话,如果我没有理解错的话,你向我解释了如何让用户无需登录即可访问我网站的某些部分。这不是完全的错误,但我希望他们不是通过经典的html表单登录,而是直接在地址中提供他们的日志信息(这不是我的选择,我必须这样做)。我在上面添加了配置xml文件,以防对您有所帮助
<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>
       /WEB-INF/spring/secu-config.xml
   </param-value>
<http use-expressions="true">
 <intercept-url pattern="/public/**" access="permitAll" />
 <!-- You can add n number of filters here-->
</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>