原文: https://howtodoinjava.com/vaadin/vaadin-spring-security-basicauth-example/

在本教程中,我们将学习在 spring security 模块提供的基本认证安全性之后保护 vaadin 应用的安全。

我正在使用 Spring 安全配置更新 vaadin helloworld 应用源代码,因此,如果您已经有任何 vaadin 应用,则可以直接查看 Spring 安全性部分。

  1. Table of Contents
  2. Development environment
  3. Spring Security BasicAuth Configuration
  4. Vaadin UI Configuration
  5. Maven Dependencies
  6. Run the application

开发环境

本示例使用以下工具和框架来构建演示 vaadin 应用,该应用在 spring 的基本认证安全性的保护下得到保护。

  1. JDK 1.8
  2. Vaadin 7.7.0
  3. Spring Security 4.1.3.RELEASE
  4. Eclipse Luna
  5. Tomcat 7

Spring Security BasicAuth 配置

要配置 spring basicauth 安全性,您需要在类路径中添加applicationContext.xml文件(如果尚不存在),然后需要配置安全性设置,例如,安全的 URL 模式,哪些角色可以访问哪些 URL 等。

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans:beans xmlns="http://www.springframework.org/schema/security"
  3. xmlns:beans="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
  7. http://www.springframework.org/schema/security
  8. http://www.springframework.org/schema/security/spring-security-4.1.xsd">
  9. <http auto-config="true">
  10. <intercept-url pattern="/vaadinServlet/**" access="hasRole('ROLE_EDITOR')" />
  11. <intercept-url pattern="/vaadinServlet/*.*" access="hasRole('ROLE_EDITOR')" />
  12. <intercept-url pattern="/**" access="hasRole('ROLE_EDITOR')" />
  13. <http-basic />
  14. <csrf disabled="true"/>
  15. </http>
  16. <authentication-manager>
  17. <authentication-provider>
  18. <user-service>
  19. <user name="howtodoinjava" password="password" authorities="ROLE_EDITOR" />
  20. </user-service>
  21. </authentication-provider>
  22. </authentication-manager>
  23. </beans:beans>

现在,您需要在web.xml文件中配置springSecurityFilterChain,以便将安全性添加到应用中。 另外,如果您添加了新的applicationContext.xml文件,则还需要注册ContextLoaderListener

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app id="WebApp_ID" version="2.4"
  3. xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  5. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  6. <filter>
  7. <filter-name>springSecurityFilterChain</filter-name>
  8. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  9. </filter>
  10. <filter-mapping>
  11. <filter-name>springSecurityFilterChain</filter-name>
  12. <url-pattern>/*</url-pattern>
  13. </filter-mapping>
  14. <listener>
  15. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  16. </listener>
  17. //Other configuration will be added here
  18. </web-app>

Spring 基本认证配置已完成。 现在,您可以根据应用的要求修改各个部分。 例如。 您可能要从数据库中获取用户名/密码详细信息,然后可以在applicationContext.xml文件的authentication-provider中使用jdbc-user-service

Vaadin UI 配置

正如我已经提到的,我正在修改 vaadin helloworld 应用,它具有非常基本的功能。 只需在web.xml文件和带有标签的主页屏幕中进行VaadinServlet配置,以在认证成功的情况下显示成功消息。

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app id="WebApp_ID" version="2.4"
  3. xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  5. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  6. //Spring security configuration as mentioned in above section
  7. <context-param>
  8. <description>Vaadin production mode</description>
  9. <param-name>productionMode</param-name>
  10. <param-value>false</param-value>
  11. </context-param>
  12. <servlet>
  13. <servlet-name>vaadinServlet</servlet-name>
  14. <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
  15. <init-param>
  16. <param-name>UI</param-name>
  17. <param-value>com.howtodoinjava.vaadin.demo.AppUI</param-value>
  18. </init-param>
  19. </servlet>
  20. <servlet-mapping>
  21. <servlet-name>vaadinServlet</servlet-name>
  22. <url-pattern>/*</url-pattern>
  23. </servlet-mapping>
  24. </web-app>

AppUI.java

  1. package com.howtodoinjava.vaadin.demo;
  2. import com.vaadin.annotations.Theme;
  3. import com.vaadin.server.VaadinRequest;
  4. import com.vaadin.ui.Label;
  5. import com.vaadin.ui.UI;
  6. import com.vaadin.ui.VerticalLayout;
  7. @Theme("mytheme")
  8. public class AppUI extends UI {
  9. private static final long serialVersionUID = 1387172685749279538L;
  10. @Override
  11. protected void init(VaadinRequest vaadinRequest) {
  12. final VerticalLayout layout = new VerticalLayout();
  13. Label label = new Label("Welcome to BasicAuth Secured Vaadin Application");
  14. layout.addComponent(label);
  15. layout.setMargin(true);
  16. layout.setSpacing(true);
  17. setContent(layout);
  18. }
  19. }

Maven 依赖

应用非常重要的一部分是收集和配置运行时依赖项。 当我们使用 maven 时,我已将以下依赖项添加到现有的pom.xml文件中。

pom.xml

  1. <!-- Spring Security -->
  2. <dependency>
  3. <groupId>org.springframework.security</groupId>
  4. <artifactId>spring-security-core</artifactId>
  5. <version>${org.springframework.version}</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework.security</groupId>
  9. <artifactId>spring-security-web</artifactId>
  10. <version>${org.springframework.version}</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework.security</groupId>
  14. <artifactId>spring-security-config</artifactId>
  15. <version>${org.springframework.version}</version>
  16. </dependency>
  17. <!-- Commons Logging is required with Spring4.x -->
  18. <dependency>
  19. <groupId>commons-logging</groupId>
  20. <artifactId>commons-logging</artifactId>
  21. <version>1.2</version>
  22. </dependency>

运行应用

现在,该应用已配置完毕,可以进行测试了。 在浏览器中点击应用 URL。

  1. 点击网址http://localhost:8080/VaadinExample/


您将在浏览器弹出窗口中输入您的用户名和密码。
Vaadin Spring Security BasicAuth 示例 - 图1
Vaadin Spring Security BasicAuth 窗口

  1. 填写不正确的凭据并提交


弹出字段将被清除,并再次询问用户名/密码。

  1. 填写正确的凭据并提交


应用的主页将显示成功消息。
Vaadin Spring Security BasicAuth 示例 - 图2
Vaadin Spring Security BasicAuth 成功

将我的问题放在评论部分。

源码下载

资源:

Spring 安全参考

Vaadin HelloWorld 应用

RFC-2617 BasicAuth