首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CAS + Spring安全检测本地主机

CAS + Spring安全检测本地主机
EN

Stack Overflow用户
提问于 2013-12-19 15:19:17
回答 3查看 1.6K关注 0票数 0

我试图用Spring安全和CAS实现一个应用程序,它在本地主机上运行良好,但是当我尝试从外部机器访问它时,应用程序也需要身份验证,它也重定向到localhost。

含义

我使用https://172.16.1.50:8443/isxannouncements/访问应用程序,当它需要身份验证时,它应该转到https://172.16.1.50:8443/cas/login/,但是它会转到https://localhost:8443/isxannouncements/

这当然会破坏应用程序流。

我的配置是

security-cas.xml

代码语言:javascript
复制
<bean id="serviceProperties"
      class="org.springframework.security.cas.ServiceProperties">
    <property name="service"
              value="https://localhost:8443/isxannouncements/login"/>
</bean>
<!--
    Allows changing where the CAS Server and CAS Service are easily
    by specifying System Arguments or replacing the values only in one place.
    Could also use external properties file -->
<context:property-placeholder
        system-properties-mode="OVERRIDE" properties-ref="environment"/>
<util:properties id="environment">
    <prop key="cas.service.host">localhost:8443</prop>
    <prop key="cas.server.host">localhost:8443</prop>
</util:properties>

<!-- sends to the CAS Server, must be in entry-point-ref of security.xml -->
<bean id="casEntryPoint"
      class="org.springframework.security.cas.web.CasAuthenticationEntryPoint">
    <property name="serviceProperties" ref="serviceProperties"/>
    <property name="loginUrl" value="https://localhost:8443/cas/login" />
</bean>

<!-- authenticates CAS tickets, must be in custom-filter of security.xml -->
<bean id="casFilter"
      class="org.springframework.security.cas.web.CasAuthenticationFilter">
    <property name="authenticationManager" ref="authenticationManager"/>
    <property name="filterProcessesUrl" value="/login"/>
</bean>

<bean id="casAuthProvider" class="org.springframework.security.cas.authentication.CasAuthenticationProvider">
    <property name="ticketValidator" ref="ticketValidator"/>
    <property name="serviceProperties" ref="serviceProperties"/>
    <property name="key" value="isxannouncements"/>
    <property name="authenticationUserDetailsService" ref="DBUserServiceDetails"/>
    <property name="statelessTicketCache" ref="statelessTicketCache"/>
</bean>

<bean id="statelessTicketCache" class="org.springframework.security.cas.authentication.EhCacheBasedTicketCache">
    <property name="cache">
        <bean class="net.sf.ehcache.Cache"
              init-method="initialise" destroy-method="dispose">
            <constructor-arg value="casTickets"/>
            <constructor-arg value="50"/>
            <constructor-arg value="true"/>
            <constructor-arg value="false"/>
            <constructor-arg value="3600"/>
            <constructor-arg value="900"/>
        </bean>
    </property>
</bean>

<bean id="ticketValidator" class="org.jasig.cas.client.validation.Saml11TicketValidator">
    <constructor-arg value="https://localhost:8443/cas" />
    <property name="encoding" value="utf8" />
</bean>

<!-- Handles a Single Logout Request from the CAS Server must be in custom-filter of security.xml -->
<bean id="singleLogoutFilter" class="org.jasig.cas.client.session.SingleSignOutFilter"/>

还有我的security.xml

代码语言:javascript
复制
<security:http pattern="/resources/images" security="none"/>
<security:http use-expressions="true" entry-point-ref="casEntryPoint">
    <security:intercept-url pattern="/login/*"
                            access="permitAll"/>
    <security:intercept-url pattern="/resources/**"
                            access="permitAll"/>
    <security:intercept-url pattern="/logout"
                            access="permitAll"/>
    <security:intercept-url pattern="/errors/**"
                            access="permitAll"/>
    <security:intercept-url pattern="/approve-announcement**"
                            access="hasRole('ROLE_USER')"/>
    <security:intercept-url pattern="/delete-announcement**"
                            access="hasRole('ROLE_USER')"/>
    <security:intercept-url pattern="/edit-announcement**"
                            access="hasRole('ROLE_USER')"/>
    <security:intercept-url pattern="/admin/**"
                            access="hasRole('ROLE_ADMIN')"/>
    <security:intercept-url pattern="/META-INF"
                            access="hasRole('ROLE_USER')"/>
    <security:access-denied-handler error-page="/errors/403"/>

    <security:custom-filter ref="singleLogoutFilter" before="LOGOUT_FILTER"/>
    <security:custom-filter ref="casFilter" position="CAS_FILTER"/>
    <security:port-mappings>
        <security:port-mapping http="8080" https="8443"/>
    </security:port-mappings>

    <security:logout logout-url="/logout"
                     logout-success-url="https://localhost:8443/cas/logout"/>
</security:http>
<security:authentication-manager alias="authenticationManager">
    <security:authentication-provider ref="casAuthProvider" />
</security:authentication-manager>

怎么解决这个??

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-12-24 08:49:45

好的,我找到了一个解决办法,但是我没有正确地测试它,

受此answer的启发,我做了以下工作

由于用户可能使用两个域访问我的应用程序,因此获得用户使用的域的唯一方法是从请求中获取域,然后将其返回给安全提供者。

我创建了一个名为serviceProperties的bean,并使用它代替了spring的serviceproperties,并重写了getService方法,以基于用户访问应用程序的域名返回服务。

然后,我在Web应用程序上下文中提供了这个bean,并在会话中传递,我已经从请求中提取了域并将其放在会话中。

因此,当CasAuthenticationEntryPoint试图获取服务时,我传递从附加到它的会话中创建的服务URL,即服务名称。

票数 1
EN

Stack Overflow用户

发布于 2013-12-19 18:57:23

我们用属性文件来处理这个问题。任何特定于特定环境的内容(即本地机器与测试服务器)都应该位于属性文件中。

例如,为每个环境创建属性文件,如下所示:

localhost.properties:

代码语言:javascript
复制
    cas.service.url=http://localhost/login

test.properties:

代码语言:javascript
复制
    cas.service.url=http://mytestserver/login

然后使用属性文件的值配置spring安全性,而不是像上面那样直接配置它:

然后,构建过程将为每个环境提供一个目标,以便将适当的文件洗牌到最终工件中。

票数 0
EN

Stack Overflow用户

发布于 2013-12-24 03:05:48

CAS在域下工作。所以您应该使用cas.example.com并在cas.properties中定义它

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20685308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档