English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

مثال XML لأمان Spring

في هذا الدليل، سنستخدمMVC Spring تحقيق إطار العمل أمان Spring.جميع الأمثلة هي Spring MVC، وهي مبنية على مشاريع Maven.

نستخدم Spring Security 5.0.0.RELEASE الإصدار، إليك إدخال الاعتماديات Maven، التي استخدمناها في جميع الأمثلة.

<dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>5.0.0.RELEASE</version>
</dependency>
<dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>5.0.0.RELEASE</version>
</dependency>
<dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>5.0.0.RELEASE</version>
</dependency>

لتحقيق أمان Spring في تطبيق Spring، يمكننا ضبطه باستخدام XML أو تكوين قائم على Java.

لننظر في مثال، حيث سيتم استخدام XML لضبط أمان Spring.

إنشاء مشروع Maven

كما نفعل، انقر ملف في القائمة جديد→مشروع Maven في الشاشة التالية.

اختر اسم المشروع والموقع

أدخل اسم المشروع

أدخل اسم المشروع، ثم اختر نوع الت打包 التالي: war(أرشيف شبكي).

إكمالهذا المشروع، سيقوم بإنشاء بنية مجلدات فارغة للمشروع، كما يلي.

Initially, it was empty. Therefore, let's create a Spring MVC application and integrate it with Spring Security.

This is the layout of our project. It includes one controller, three XML files, and two JSP files.

Spring Security Project source code

The name of our project is springsecurity ،which includes the following source files.

Controller

HomeController. Java

package com.w3codebox.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HomeController {
    
    @RequestMapping(value="/", method=RequestMethod.GET)
    public String home() {
        return \
    }
    
    @RequestMapping(value="/admin", method=RequestMethod.GET)
    public String privateHome() {
        return \
    }
}

Spring security configuration

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.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security.xsd">
    <http auto-config="true">
        <intercept-url pattern="/admin" access="hasRole('ROLE_ADMIN')" />
    </http>
    <authentication-manager>
      <authentication-provider>
        <user-service>
        <user name="admin" password="1234" authorities="hasRole(ROLE_ADMIN)" />
        </user-service>
      </authentication-provider>
    </authentication-manager>
</beans:beans>

Servlet调度程序

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven />
   <context:component-scan base-package="com.w3codebox.controller">
   </context:component-scan>
   <context:annotation-config></context:annotation-config>
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/views/"></property>
      <property name="suffix" value=".jsp"></property>
   </bean>
</beans>

Web descriptor

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
        
        <!-- Spring Configuration -->
        <servlet>
            <servlet-name>spring</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>spring</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        
        <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <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>
        
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/spring-servlet.xml
                /WEB-INF/spring-security.xml
            </param-value>
        </context-param>
</web-app>

مستندات التبعية

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.w3codebox</groupId>
  <artifactId>springsecurity</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>   
<properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencies>
  <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>5.0.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>5.0.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>5.0.0.RELEASE</version>
    </dependency>
        
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

استعرض الصفحة

home.jsp

<html>
<head>
<meta content="text/html; charset=UTF-8">
<title>الصفحة الرئيسية</title>
</head>
<body>
<h2>مرحبًا بكم في دروس w3codebox spring!</h2>
</body>
</html>

privatePage.jsp

home.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>مدير</title>
</head>
<body>
مرحبًا، مدير
</body>
</html>

الناتج

هذا المثال يستخدم Apache Tomcat v9.0. بعد تشغيله، سيقوم بتوليد الناتج التالي للتصفح.

في البداية، سيتم عرض home.jsp الصفحة، حيث سيتم عرض الناتج التالي.

إذا قمنا بإدخال / في صفحة الإدارة admin ، سيتم إضافة أمان Spring إلى صفحة الإدارة. سيقوم المتصفح، بتوليد الناتج التالي.

URL الطلب: http: //localhost: 8080/springsecurity/admin

الآن، هذا هو السحر الحقيقي لحماية الموارد التي يقدمها أمان Spring.

هذا هو المكون الذي يقدمه أمان Spring، لم ننشئه. سيتم أيضًا التحقق من إدخال المستخدم.

تقديم بيانات اعتماد خاطئة.

إذا قمنا بتقديم بيانات اعتماد خاطئة، سيستخدم ذلك ما قمنا بتقديمه في spring-security.xml تحقق من اسم المستخدم وكلمة المرور المذكورة في الملف.

بعد التحقق، إذا كانت بيانات الاعتماد غير صحيحة، سيتم إطلاق رسالة خطأ.

في هذا المثال، رأينا مكون تسجيل الدخول لأمان Spring وكيف يتم التحقق من صحة اسم المستخدم وكلمة المرور المقدمة.

الآن، سنقوم بإنجاز منطق الموضوع بشكل أعمق، مثل: عرض المستخدم بعد تسجيل الدخول الناجح.