XML-конфигурация Spring Security не защищает URL - PullRequest
0 голосов
/ 11 октября 2018

Я прошел через множество вопросов, подобных этому, но не смог найти никакого решения.

Я использую Spring-3.0.5RELEASE и Spring-security-3.1.2RELEASE.На самом деле я добавляю весеннюю защиту в уже существующее приложение.Нет ошибок при создании bean-компонента или фильтров, но URL-адреса не защищены.

spring-security.xml выглядит следующим образом:

   <?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:security="http://www.springframework.org/schema/security"
 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-3.1.xsd">

 <security:http auto-config="true" use-expressions="true">

<!--  <intercept-url pattern="/" access="permitAll" /> -->

 <security:intercept-url pattern="/index" access="permitAll" />

 <security:intercept-url pattern="/admin"
access="hasRole('Admin')" />

 <security:intercept-url pattern="/dashboard" access="hasRole('Admin')
or hasRole('User')" />

<security:intercept-url pattern="/setup" access="hasRole('User')" />

 <!-- access denied page -->
 <security:access-denied-handler error-page="/logout" />

 <security:form-login 
 login-processing-url="/loginAuth"
 login-page="/index" 
 default-target-url="/dashboard" 
 username-parameter="username"
 password-parameter="password"
 authentication-failure-url="/index"/>
 <!-- enable csrf protection -->
<!-- <csrf/> -->
 <http-basic />
 </security:http>


 <!-- Select users and user_roles from database -->
<security:authentication-manager>
 <security:authentication-provider>
 <security:jdbc-user-service data-source-ref="dataSource"
 users-by-username-query=
 "select customerId, passcode from Users where customerId=?"
 authorities-by-username-query=
 "select customerId, roleName from Role where customerId=?" />
 </security:authentication-provider>
 </security:authentication-manager>
</beans:beans>

Здесь есть предупреждение: Ссылочный компонент "источник данных" не найден .Я не уверен, может ли это вызвать проблему.

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"
        xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        version="2.5">

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

    <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>
    <servlet>
        <servlet-name>sample</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
       <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/config/sample-servlet.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>sample</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/config/spring-security.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

sample-servlet.xml

    <context:property-placeholder location="classpath:resources/database.properties" />
    <context:component-scan base-package="com.as.spark" />

    <tx:annotation-driven transaction-manager="hibernateTransactionManager"/>

    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${database.driver}" />
        <property name="url" value="${database.url}" />
        <property name="username" value="${database.user}" />
        <property name="password" value="${database.password}" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.as.spark.model.Users</value>
                <value>com.as.spark.model.Role</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <!-- properties -->
        </property>
    </bean>

    <bean id="hibernateTransactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
</beans>

Я не хочуперейти к конфигурации Java, потому что это требует более высокой версии Spring.

Все страницы / home, / dashboard, / admin открыты для всех пользователей.Как проверить, применяется ли фильтр?Как защитить URL?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...