Spring Security Не применяется с моими URL - PullRequest
0 голосов
/ 04 мая 2018

Я установил новое Spring Web MVC-приложение и ввел в него Spring-security. Это конфигурация, которую я использовал для безопасности пружин

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

<!-- enable use-expressions -->
<http auto-config="true" use-expressions="true">
    <intercept-url pattern="/login" access="permitAll"/>
    <intercept-url pattern="/**"    access="isAuthenticated()" />
    <form-login
            login-page="/login"
            authentication-failure-url="/login?error"
            username-parameter="username"
            password-parameter="password" />
    <logout logout-success-url="/login?logout"  />
</http>

<!-- Select users and user_roles from database -->
<authentication-manager>
    <authentication-provider>
        <jdbc-user-service data-source-ref="datasource"
                           users-by-username-query=
                                   "select username,password, enabled from user where username=?"
                            />
    </authentication-provider>
</authentication-manager>

Я также использую Hibernate 5 для доступа к моей базе данных

ApplicationContext-db.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:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

<context:property-placeholder location="classpath:db/database.properties"/>
<context:component-scan base-package="com.medkhelifi.tutorials.spring.springregistration" />
<context:annotation-config />

<bean id="userDao" class="com.medkhelifi.tutorials.spring.springregistration.dao.impl.UserDaoImpl"/>

<!-- Hibernate Factory -->
<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.username}"/>
    <property name="password" value="${database.password}"/>
</bean>

<bean id="hibernateFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="datasource"/>
    <property name="annotatedClasses">
        <list>
            <value>com.medkhelifi.tutorials.spring.springregistration.model.User</value>
        </list>
    </property>
</bean>

<bean id="transcationManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="hibernateFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transcationManager" />

И я создал две страницы JSP с двумя контроллерами:

  • / JSP / login.jsp
  • / JSP / hello.jsp

Когда я запускаю свое приложение, я могу без ограничений посещать мой index.jsp и /jsp/hello.jsp. Я не знаю, что-то упустил.

(извините за мой плохой английский)

1 Ответ

0 голосов
/ 21 марта 2019

Это старый вопрос, но я хотел бы упомянуть основную причину, почему то же самое не сработало для меня:

Я также использовал конфигурацию на основе XML, но казалось, что Spring Security вообще не вызывали, потому что там даже не было заголовков по умолчанию. Это было вызвано тем, что я не добавил Spring DelegatingFilterProxy в web.xml, как описано здесь: https://docs.spring.io/spring-security/site/docs/5.0.x/reference/html/ns-config.html#ns-web-xml

После добавления фильтра Spring Security начал устанавливать заголовки по умолчанию, и я мог продолжить добавлять свои пользовательские требования.

С уважением

Стеф

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