【问题标题】:Vaadin 7.1 + Spring-Security Integration running in Tomcat ServerVaadin 7.1 + Spring-Security Integration 在 Tomcat 服务器中运行
【发布时间】:2013-09-07 11:48:38
【问题描述】:

我是 vaadin 和 spring 安全的新手,我想知道是否有人有一个完整的项目示例,说明 vaadin 7.1 + spring-security 集成在 tomcat 服务器(不是在码头)中运行。

【问题讨论】:

    标签: spring spring-security vaadin vaadin7


    【解决方案1】:

    Vaadin 7 与 Spring Security 轻松集成。您应该只配置 2 个文件。第一个 - web.xml 和第二个 spring-security.xml(用户凭据和安全设置)。这是如何使用基本形式进行身份验证的小示例。

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>Vaadin7SpringSecurity</display-name>
    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/spring-security.xml
        </param-value>
    </context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- filter declaration for Spring Security -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    </web-app>
    

    spring-security.xml

    <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.2.xsd
          http://www.springframework.org/schema/security
          http://www.springframework.org/schema/security/spring-security-3.1.xsd">
    
    <http auto-config='true'>
      <intercept-url pattern="/*" access="ROLE_USER" />
    </http>
    
    <authentication-manager>
      <authentication-provider>
        <user-service>
          <user name="user" password="password" authorities="ROLE_USER" />
        </user-service>
      </authentication-provider>
    </authentication-manager>  
    
    </beans:beans>
    

    更多细节,如何扩展spring-security.xml配置可以使用Spring resources。

    【讨论】:

    • 实际上它远非“简单”——你展示的是一个支持 vaadin 的 SPRING 应用程序——但实际上构建一个支持 SPRING SECURITY 的 VAADIN 应用程序......有点困难:vaadin.com/… - 抱歉,但您的回答仅适用于少数非常有限的情况......对于想要使用 vaadin CDI 的人来说,这真的很痛苦
    【解决方案2】:

    你应该看看这个GitHub project。这是一个 Vaadin 7.1 + Spring 3.1.2.RELEASE + Spring-Vaadin 集成 2.0.1 项目。里面还有一个 Jetty 插件,但是你也可以在 tomcat 中运行/部署它,没有问题。

    【讨论】:

    • 感谢您的回答,我已经使用了您提到的项目,但我不知道为什么当我输入凭据并单击登录按钮时,我被转发到相同的登录页面而不是 UI
    【解决方案3】:

    Here 是一个集成 Vaadin 和 Spring Security 的小项目。它是在 Scala 中完成的,但显然也适用于 Java。代码是here。

    【讨论】:

    • 感谢您的回复,我查看了代码并尝试将其翻译为 java,但我不知道 scala,所以我遇到了一些问题。我翻译了几乎所有的类,我唯一​​怀疑如何翻译的类是 NavigationErrorHandlingStrategy 和 NavigatorFactory,你能帮我把这两个类翻译成 java 吗?
    【解决方案4】:

    使用最新的spring-security参考上面的例子,遇到以下错误并提供我的建议:

    错误1

    Context initialization failed
    org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: You cannot use a spring-security-2.0.xsd or spring-security-3.0.xsd or spring-security-3.1.xsd schema or spring-security-3.2.xsd schema with Spring Security 4.0. Please update your schema declarations to the 4.0 schema.
    

    您应该检查您的 spring-* 版本并更新 spring-security.xml 的标头标签。 例如:我使用 spring-beans-4.1.6.RELEASE 和 spring-security-4.0.2.RELEASE。所以我将其更新为:

    <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-4.1.xsd
          http://www.springframework.org/schema/security
          http://www.springframework.org/schema/security/spring-security-4.0.xsd">
    

    错误2

    HTTP Status 500 - Failed to evaluate expression 'ROLE_USER'
    ...
    Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'ROLE_USER' cannot be found on object of type 'org.springframework.security.web.access.expression.WebSecurityExpressionRoot' - maybe not public?
    ...
    

    根据resource的提示,你应该修改intercept-url标签如下:

    <intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
    

    错误3

    HTTP Status 403 - Expected CSRF token not found. Has your session expired?
    

    这是因为 spring-security 默认启用 CSRF 保护,这与 Vaadin 冲突。您应该在 http 中添加一个新标签:

    <csrf disabled="true" />
    

    这是我完整的 spring-security.xml 供参考:

    <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-4.1.xsd
          http://www.springframework.org/schema/security
          http://www.springframework.org/schema/security/spring-security-4.0.xsd">
    
        <http auto-config='true'>
            <intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
            <csrf disabled="true" />
        </http>
    
        <authentication-manager>
            <authentication-provider>
                <user-service>
                    <user name="yourUsername" password="yourPassoword" authorities="ROLE_USER" />
                </user-service>
            </authentication-provider>
        </authentication-manager>
    
    </beans:beans>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-02
      • 1970-01-01
      • 2018-06-23
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      相关资源
      最近更新 更多