gpt4 book ai didi

java - 从 Spring Security 成功登录时不会调用 Spring MVC Controller 方法 - 获取 404 状态

转载 作者:太空宇宙 更新时间:2023-11-04 13:53:46 25 4
gpt4 key购买 nike

我正在尝试使用 Spring Security 配置 Spring MVC。我有用于身份验证的自定义登录页面。成功验证用户身份后,我希望在 WelcomeController.java 中调用名为 home() 的 Controller 方法

下面是我的 web.xml、test-serlet.xml、test-security-config.xml、pom.xml、WelcomeController.java 和日志文件。

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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>test</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/test-servlet.xml
/WEB-INF/test-security-config.xml
</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>test</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/test-servlet.xml
/WEB-INF/test-security-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>

<!--Spring security filter -->
<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>

</web-app>

测试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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:annotation-config />
<context:component-scan base-package="com.test.web"/>
<mvc:annotation-driven />
<mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/pages/" p:suffix=".jsp" />
</beans>

测试安全配置.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"
xmlns:p="http://www.springframework.org/schema/p"
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">



<http auto-config="true" use-expressions="true" access-denied-page="/welcome/denied">

<intercept-url pattern="/welcome/*" access="hasAnyRole('ROLE_SUPERADMIN','ROLE_OEMMANAGER','ROLE_OEMSALESPERSON')"/>
<intercept-url pattern="/index*" access="permitAll"/>
<intercept-url pattern="/login*" access="isAnonymous()" method="GET"/>
<intercept-url pattern="/**" access="isAuthenticated()" />
<form-login login-page="/login.jsp" login-processing-url="/login" default-target-url="/welcome/home" />
<logout logout-success-url="/index.jsp"/>

</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="shuchi" authorities="ROLE_SUPERADMIN" password="a" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>

WelcomeController.java

package com.test.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/welcome")
public class WelcomeController {
private static Log log = LogFactory.getLog(WelcomeController.class);

@RequestMapping(value = { "/home" }, method = RequestMethod.GET)
public String home(Model model) {
log.info("--- home() Starts ---");
System.out.println("--- home() Starts ---");
Object principal = SecurityContextHolder.getContext()
.getAuthentication().getPrincipal();
String username = "";
if (principal instanceof UserDetails) {
username = ((UserDetails) principal).getUsername();
} else {
username = principal.toString();
}
log.info("username=" + username);
model.addAttribute("message",
"Hi Tehre! You've reached the welcome page!");
// ModelAndView model = new ModelAndView("index");
return "home";
}

@RequestMapping(value = "/denied", method = RequestMethod.GET)
public String getDeniedPage() {
// log.info("User's role is: "+ <sec:authentication
// property="principal.authorities"/>);
Object principal = SecurityContextHolder.getContext()
.getAuthentication().getPrincipal();
String username = "";
if (principal instanceof UserDetails) {
username = ((UserDetails) principal).getUsername();
} else {
username = principal.toString();
}
log.info("username=" + username);
log.debug("Received request to show denied page");

// This will resolve to /WEB-INF/jsp/deniedpage.jsp
return "denied";
}
}

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>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-acl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging-api</artifactId>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-acl</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>3.6.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.5.Final</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>3.6.10.Final</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>3.6.10.Final</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

测试.log

...

    2015-05-04 12:21:27 DEBUG AntPathRequestMatcher:145 - Checking match of request : '/login.jsp'; against '/login*'
2015-05-04 12:21:27 DEBUG FilterSecurityInterceptor:194 - Secure object: FilterInvocation: URL: /login.jsp; Attributes: [isAnonymous()]
2015-05-04 12:21:27 DEBUG FilterSecurityInterceptor:310 - Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@9056f12c: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@380f4: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: C51942A05166FA866F8863CA07809883; Granted Authorities: ROLE_ANONYMOUS
2015-05-04 12:21:27 DEBUG AffirmativeBased:65 - Voter: org.springframework.security.web.access.expression.WebExpressionVoter@5875e564, returned: 1
2015-05-04 12:21:27 DEBUG FilterSecurityInterceptor:215 - Authorization successful
2015-05-04 12:21:27 DEBUG FilterSecurityInterceptor:227 - RunAsManager did not change Authentication object
2015-05-04 12:21:27 DEBUG FilterChainProxy:323 - /login.jsp reached end of additional filter chain; proceeding with original chain
2015-05-04 12:21:28 DEBUG ExceptionTranslationFilter:115 - Chain processed normally
2015-05-04 12:21:28 DEBUG HttpSessionSecurityContextRepository:304 - SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
2015-05-04 12:21:28 DEBUG SecurityContextPersistenceFilter:97 - SecurityContextHolder now cleared, as request processing completed
2015-05-04 12:21:32 DEBUG FilterChainProxy:337 - /login at position 1 of 11 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
2015-05-04 12:21:32 DEBUG HttpSessionSecurityContextRepository:152 - HttpSession returned null object for SPRING_SECURITY_CONTEXT
2015-05-04 12:21:32 DEBUG HttpSessionSecurityContextRepository:91 - No SecurityContext was available from the HttpSession: org.apache.catalina.session.StandardSessionFacade@15ce9113. A new one will be created.
2015-05-04 12:21:32 DEBUG FilterChainProxy:337 - /login at position 2 of 11 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter'
2015-05-04 12:21:32 DEBUG FilterChainProxy:337 - /login at position 3 of 11 in additional filter chain; firing Filter: 'LogoutFilter'
2015-05-04 12:21:32 DEBUG FilterChainProxy:337 - /login at position 4 of 11 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter'
2015-05-04 12:21:32 DEBUG UsernamePasswordAuthenticationFilter:205 - Request is to process authentication
2015-05-04 12:21:32 DEBUG ProviderManager:152 - Authentication attempt using org.springframework.security.authentication.dao.DaoAuthenticationProvider
2015-05-04 12:21:32 DEBUG CompositeSessionAuthenticationStrategy:81 - Delegating to org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy@cc026e6
2015-05-04 12:21:32 DEBUG SessionFixationProtectionStrategy:87 - Invalidating session with Id 'C51942A05166FA866F8863CA07809883' and migrating attributes.
2015-05-04 12:21:32 DEBUG SessionFixationProtectionStrategy:97 - Started new session: 4F86F4424F09507605F075E2A8F90748
2015-05-04 12:21:32 DEBUG UsernamePasswordAuthenticationFilter:319 - Authentication success. Updating SecurityContextHolder to contain: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@6124f8c9: Principal: org.springframework.security.core.userdetails.User@ca2d8be4: Username: shuchi; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_SUPERADMIN; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@380f4: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: C51942A05166FA866F8863CA07809883; Granted Authorities: ROLE_SUPERADMIN
2015-05-04 12:21:32 DEBUG SavedRequestAwareAuthenticationSuccessHandler:107 - Using default Url: /welcome/home
2015-05-04 12:21:32 DEBUG DefaultRedirectStrategy:36 - Redirecting to '/test/welcome/home'
2015-05-04 12:21:32 DEBUG HttpSessionSecurityContextRepository:327 - SecurityContext stored to HttpSession: 'org.springframework.security.core.context.SecurityContextImpl@6124f8c9: Authentication: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@6124f8c9: Principal: org.springframework.security.core.userdetails.User@ca2d8be4: Username: shuchi; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_SUPERADMIN; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@380f4: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: C51942A05166FA866F8863CA07809883; Granted Authorities: ROLE_SUPERADMIN'
2015-05-04 12:21:32 DEBUG SecurityContextPersistenceFilter:97 - SecurityContextHolder now cleared, as request processing completed
2015-05-04 12:21:32 DEBUG FilterChainProxy:337 - /welcome/home at position 1 of 11 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
2015-05-04 12:21:32 DEBUG HttpSessionSecurityContextRepository:171 - Obtained a valid SecurityContext from SPRING_SECURITY_CONTEXT: 'org.springframework.security.core.context.SecurityContextImpl@6124f8c9: Authentication: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@6124f8c9: Principal: org.springframework.security.core.userdetails.User@ca2d8be4: Username: shuchi; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_SUPERADMIN; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@380f4: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: C51942A05166FA866F8863CA07809883; Granted Authorities: ROLE_SUPERADMIN'
2015-05-04 12:21:32 DEBUG FilterChainProxy:337 - /welcome/home at position 2 of 11 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter'
2015-05-04 12:21:32 DEBUG FilterChainProxy:337 - /welcome/home at position 3 of 11 in additional filter chain; firing Filter: 'LogoutFilter'
2015-05-04 12:21:32 DEBUG FilterChainProxy:337 - /welcome/home at position 4 of 11 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter'
2015-05-04 12:21:32 DEBUG FilterChainProxy:337 - /welcome/home at position 5 of 11 in additional filter chain; firing Filter: 'BasicAuthenticationFilter'
2015-05-04 12:21:32 DEBUG FilterChainProxy:337 - /welcome/home at position 6 of 11 in additional filter chain; firing Filter: 'RequestCacheAwareFilter'
2015-05-04 12:21:32 DEBUG FilterChainProxy:337 - /welcome/home at position 7 of 11 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'
2015-05-04 12:21:32 DEBUG FilterChainProxy:337 - /welcome/home at position 8 of 11 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter'
2015-05-04 12:21:32 DEBUG AnonymousAuthenticationFilter:107 - SecurityContextHolder not populated with anonymous token, as it already contained: 'org.springframework.security.authentication.UsernamePasswordAuthenticationToken@6124f8c9: Principal: org.springframework.security.core.userdetails.User@ca2d8be4: Username: shuchi; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_SUPERADMIN; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@380f4: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: C51942A05166FA866F8863CA07809883; Granted Authorities: ROLE_SUPERADMIN'
2015-05-04 12:21:32 DEBUG FilterChainProxy:337 - /welcome/home at position 9 of 11 in additional filter chain; firing Filter: 'SessionManagementFilter'
2015-05-04 12:21:32 DEBUG FilterChainProxy:337 - /welcome/home at position 10 of 11 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
2015-05-04 12:21:32 DEBUG FilterChainProxy:337 - /welcome/home at position 11 of 11 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
2015-05-04 12:21:32 DEBUG AntPathRequestMatcher:145 - Checking match of request : '/welcome/home'; against '/welcome/*'
2015-05-04 12:21:32 DEBUG FilterSecurityInterceptor:194 - Secure object: FilterInvocation: URL: /welcome/home; Attributes: [hasAnyRole('ROLE_SUPERADMIN','ROLE_OEMMANAGER','ROLE_OEMSALESPERSON')]
2015-05-04 12:21:32 DEBUG FilterSecurityInterceptor:310 - Previously Authenticated: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@6124f8c9: Principal: org.springframework.security.core.userdetails.User@ca2d8be4: Username: shuchi; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_SUPERADMIN; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@380f4: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: C51942A05166FA866F8863CA07809883; Granted Authorities: ROLE_SUPERADMIN
2015-05-04 12:21:33 DEBUG AffirmativeBased:65 - Voter: org.springframework.security.web.access.expression.WebExpressionVoter@5875e564, returned: 1
2015-05-04 12:21:33 DEBUG FilterSecurityInterceptor:215 - Authorization successful
2015-05-04 12:21:33 DEBUG FilterSecurityInterceptor:227 - RunAsManager did not change Authentication object
2015-05-04 12:21:33 DEBUG FilterChainProxy:323 - /welcome/home reached end of additional filter chain; proceeding with original chain
2015-05-04 12:21:33 DEBUG ExceptionTranslationFilter:115 - Chain processed normally
2015-05-04 12:21:33 DEBUG SecurityContextPersistenceFilter:97 - SecurityContextHolder now cleared, as request processing completed

我真的很感谢对此的任何帮助。谢谢!

最佳答案

您将 Controller 请求映射与根上下文混合,您的 Controller 应该是:

@Controller
@RequestMapping("/welcome")
public class WelcomeController {
private static Log log = LogFactory.getLog(WelcomeController.class);

@RequestMapping(value = "/home", method = RequestMethod.GET)
public String home(Model model) {
...
}

@RequestMapping(value = "/denied", method = RequestMethod.GET)
public String getDeniedPage() {
...
}
}

在安全配置中,将拒绝访问页面更改为“/welcome/denied”:

<http auto-config="true" use-expressions="true" access-denied-page="/welcome/denied">

<intercept-url pattern="/welcome/*" access="hasAnyRole('ROLE_SUPERADMIN','ROLE_OEMMANAGER','ROLE_OEMSALESPERSON')"/>
<intercept-url pattern="/index*" access="permitAll"/>
<intercept-url pattern="/login*" access="isAnonymous()" method="GET"/>
<intercept-url pattern="/**" access="isAuthenticated()" />
<form-login login-page="/login.jsp" login-processing-url="/login" default-target-url="/welcome/home" />
<logout logout-success-url="/index.jsp"/>

</http>

关于java - 从 Spring Security 成功登录时不会调用 Spring MVC Controller 方法 - 获取 404 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30038689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com