Я нашел много похожих вопросов, но ни один из них не решил мою проблему. // весенний файл безопасности, когда я использую аннотацию уровня метода @PreAuthorize ("hasRole ('ROLE_ADMIN')"), он всегда печатает anonmousUser. У меня есть отдельный файл для applicationContext.
Уровень безопасности Spring @PreAuthorize игнорируется весенней безопасностью, введите описание изображения здесь. Моя весенняя безопасность. xml код следующий. [Скриншот кода] [1]
//aspsecurity-servlet.xml
<bean>
<context:component-scan base-package="com.asp.*" />
<!-- Getting Database properties -->
<context:property-placeholder location="classpath:application.properties" />
<mvc:annotation-driven />
<mvc:resources mapping="/**" location="/" />
<mvc:resources location="/**" mapping="/css/**" />
<mvc:resources location="/**" mapping="/js/**" />
<mvc:view-controller path="/" view-name="index" />
<!-- Resolves Views Selected For Rendering by @Controllers to *.jsp Resources in the /WEB-INF/ Folder -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
[1]: https://i.stack.imgur.com/teuci.png
//security.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans>
<global-method-security pre-post-annotations="enabled"/>
<!-- spring security configuration -->
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/login" access="permitAll" />
<intercept-url pattern="/domain*" access="hasRole('ROLE_ADMIN')" />
<!-- user-defined login form redirection -->
<form-login login-page="/login" default-target-url="/domain/"
authentication-failure-url="/login?error" />
<!-- logout url -->
<logout logout-success-url="/login?logout" />
<!-- csrf disabled -->
<csrf disabled="true" />
</http>
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query="select user_name, password, enabled from USERS where user_name = ?"
authorities-by-username-query="select u.user_name, ur.authority from USERS u, USERS_ROLES ur where u.user_id = ur.user_id and u.user_name = ?" />
</authentication-provider>
</authentication-manager>
<!-- database configuration (database = MySql) -->
<beans:bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName"
value="com.mysql.cj.jdbc.Driver" />
<beans:property name="url"
value="jdbc:mysql://localhost:3306/asp" />
<beans:property name="username" value="root" />
<beans:property name="password" value="root" />
</beans:bean>
<!-- Hibernate SessionFactory -->
<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource"></beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">${hibernate.dialect}</beans:prop>
<beans:prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</beans:prop>
<beans:prop key="hibernate.format_sql">${hibernate.format_sql}</beans:prop>
<beans:prop key="hibernate.show_sql">${hibernate.show_sql}</beans:prop>
</beans:props>
</beans:property>
<beans:property name="packagesToScan" value="com.asp.model"></beans:property>
</beans:bean>
<!-- Transaction -->
<beans:bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory" />
</beans:bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans:beans>
//web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Airport support app</display-name>
<!-- spring configuration - process the application requests -->
<servlet>
<servlet-name>aspsecurity</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>aspsecurity</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<session-config>
<session-timeout>15</session-timeout>
</session-config>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/aspsecurity-servlet.xml
/WEB-INF/security.xml
</param-value>
</context-param>
<!-- spring security configuration -->
<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>
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
//Java
@PreAuthorize("hasRole('ROLE_ADMIN')")
public ModelAndView getAddDomain() {
}