java - Spring Security 未应用于我的 URL

标签 java spring jsp spring-security

我设置了一个新的 Spring Web MVC 应用程序,并为其引入了 spring-security。 这是我用于 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" />

我创建了两个带有两个 Controller 的 jsp 页面:

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

当我运行我的应用程序时,我可以不受任何限制地访问我的index.jsp 和我的/jsp/hello.jsp。我不知道我是否遗漏了一些东西。

(抱歉我的英语不好)

最佳答案

这是一个老问题,但我想提一下为什么同样的问题对我不起作用的根本原因:

我还使用了基于 XML 的配置,但似乎根本没有调用 Spring Security,因为甚至没有默认 header 。 这是因为我没有将 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 开始设置默认 header ,我可以继续添加自定义要求。

亲切的问候

史蒂夫

关于java - Spring Security 未应用于我的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50175885/

相关文章:

java - 迭代嵌入链接时出现 StaleElementReferenceException

java - 打印出包含另一个 HashMap 的 HashMap

java - MySQLSyntaxErrorException 尽管一切看起来都很好(Hibernate)

jsp - wamp 服务器 :How to deploy jsp application

java - 什么样的参数分配给 'HTTPServletResponse response'

java - 使用 Java 和 SAML 2.0 的 Web SSO

android - 如何将 MappingJacksonHttpMessageConverter 的内容类型从 application/json;charset=UTF-8 更改为 application/json

java - 如何使用 Spring 使用 Maven Java 元素链接到外部 css 样式表

java - 在 Spring RedisTemplate 中使用 TTL 的 Redis MSET

java - jsp中重音字符的问题