【问题标题】:Spring Security: SecurityContextHolder.getContext().getAuthentication() returns null on Wicket PageSpring Security:SecurityContextHolder.getContext().getAuthentication() 在 Wicket 页面上返回 null
【发布时间】:2012-07-02 00:53:24
【问题描述】:

我在 Google App Engine 上使用 Spring MVC(用于 REST)、Spring Security 3 和 Apache Wicket (UI)。一切正常,除了登录后我无法通过 SecurityContextHolder 在 Wicket 页面上获取身份验证。

我已经用谷歌搜索了这个问题,但似乎没有一个对我有用。我怀疑这是我的 web xml 有问题。任何人都可以请帮忙。谢谢。

我正在使用来自 http://blog.springsource.org/2010/08/02/spring-security-in-google-app-engine/ 的 Google App Engine 上的 Spring Security 教程

这是我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>        
 <display-name>MTP Portal</display-name>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/mtp-web-servlet.xml, /WEB-INF/mtp-web-security-context.xml
    </param-value>
</context-param>

<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>

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

<servlet>
    <servlet-name>mtp-web</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>mtp-web</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>WicketApp</filter-name>
    <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
    <init-param>
        <param-name>applicationFactoryClassName</param-name>
        <param-value>org.apache.wicket.spring.SpringWebApplicationFactory</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>WicketApp</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

这是我的 spring 安全配置:

<?xml version="1.0" encoding="UTF-8"?>
<b:beans xmlns="http://www.springframework.org/schema/security"
     xmlns:b="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.1.xsd">

<global-method-security pre-post-annotations="enabled"/>

<http pattern="/images/**" security="none"/>
<http pattern="/css/**" security="none"/>
<http pattern="/js/**" security="none"/>
<http pattern="/api/**" security="none"/>
<http pattern="/favicon.ico" security="none"/>
<http pattern="/disabled" security="none"/>

<http use-expressions="true" entry-point-ref="gaeEntryPoint" auto-config="true">
    <intercept-url pattern="/" access="permitAll"/>
    <intercept-url pattern="/api/**" access="permitAll"/>
    <intercept-url pattern="/admin/logout" access="permitAll"/>
    <intercept-url pattern="/register" access="hasRole('NEW_USER')"/>
    <intercept-url pattern="/admin/**" access="hasRole('ADMIN')"/>
    <custom-filter position="PRE_AUTH_FILTER" ref="gaeFilter"/>
</http>

<b:bean id="gaeEntryPoint"
        class="com.peerbuccoss.apps.mtp.web.authentication.impl.GoogleAccountsAuthenticationEntryPoint"/>

<b:bean id="gaeFilter" class="com.peerbuccoss.apps.mtp.web.authentication.filter.GaeAuthenticationFilter">
    <b:property name="authenticationManager" ref="authenticationManager"/>
    <b:property name="failureHandler">
        <b:bean class="org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler">
            <b:property name="exceptionMappings">
                <b:map>
                    <b:entry key="org.springframework.security.authentication.DisabledException"
                             value="/disabled"/>
                </b:map>
            </b:property>
        </b:bean>
    </b:property>
</b:bean>

<authentication-manager alias="authenticationManager">
    <authentication-provider ref="gaeAuthenticationProvider"/>
</authentication-manager>

<b:bean id="gaeAuthenticationProvider"
        class="com.peerbuccoss.apps.mtp.web.authentication.provider.GoogleAccountsAuthenticationProvider"/>

【问题讨论】:

  • 如果我从 更改拦截 URL,我能够从安全上下文中获取身份验证对象> 到

标签: spring google-app-engine spring-security wicket-1.5


【解决方案1】:

我不确定哪个 URL 无法获取 SecurityContext(也许您可以提供一个示例 URL),但不会为任何映射到 security="none" 的 URL 填充 SecurityContext。这是因为 security="none" 指示 Spring Security 完全忽略此 URL。如果您需要访问每个用户都允许的 URL 上的 SecurityContext,那么您需要使用 permitAll。

PS:如果这没有帮助,您可以提供一个示例 URL,说明您在获取身份验证时遇到了问题。您还可以详细说明“在 Wicket 页面上获取身份验证时遇到问题”的含义(即它是否为空、抛出异常等)。

【讨论】:

  • 感谢您的回复。基本上,我试图通过以下 url localhost:8080/admin 访问管理页面,它将我重定向到 Google 登录页面。使用电子邮件帐户(@peerbuccoss.com)成功登录后,如果未找到该电子邮件,我将重定向到注册页面以提示用户输入其个人信息以完成注册过程,否则应重定向到管理页面.登录成功,但是在注册页面上调用 SecurityContextHolder.getContext().getAuthentication() 时我得到了 null。 localhost:8080/register.
  • 我添加了一个新角色,所以我的页面重定向到注册页面。 .在这里,您可以看到所指的代码示例 (github.com/SpringSource/spring-security/tree/master/samples/gae)。但它使用的是 Spring MVC + Security 和 GAE。对于弹簧检票口,我有一些额外的过滤器。
  • 我遇到了同样的问题,我的 register.htm(jsp) 给了我一个 403 错误。你是怎么解决这个问题的,因为我的认证用户有权限={NEW_USER}
猜你喜欢
  • 2015-09-01
  • 2016-07-24
  • 2013-07-08
  • 2014-04-07
  • 2021-12-23
  • 2015-08-17
  • 2014-06-15
  • 2013-06-01
相关资源
最近更新 更多