从Spring安全性上下文访问应用程序上下文

davvs

我有一个使用Spring安全性进行授权的Spring MVC应用程序。

我已经实现了一个自定义的AuthenticationProvider来授权用户。

我希望此自定义AuthenticationProvider访问在应用程序上下文中定义的bean。

这可能吗?如果是这样,怎么办?

web.xml:

 ...
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>WEB-INF/spring-security.xml</param-value>
 </context-param>
  ...
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
  ...
 <servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>

spring-security.xml:

  ...
  <authentication-manager>
    <authentication-provider ref="customAuthenticationProvider"/>
  </authentication-manager>

  <beans:bean class="com.example.davvstest.CustomAuthenticationProvider" id="customAuthenticationProvider">
    <beans:property name="loginService" ref="loginService" />
  </beans:bean>
  ...

dispatcher-servlet.xml:

  <bean class="com.example.davvstest.LoginService" name="loginService">
  </bean>

CustomAuthenticationProvider.java:

package com.example.davvstest;

import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;

public class CustomAuthenticationProvider implements AuthenticationProvider {

    private LoginService loginService;

    public LoginService getLoginService() {
        return loginService;
    }

    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }

    @Override
    public Authentication authenticate(Authentication authentication)
            throws AuthenticationException {
        if (!loginService.checkAuth(authentication.getName())){
            throw new BadUserNameException("Bad username");
        }
        return authentication;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return true;
    }

}

LoginService.java:

package com.example.davvstest;

public class LoginService {

    public LoginService() {
    }

    public boolean checkAuth(String username){
        return true;
    }
}

我得到的错误是:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.filterChains': Cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#0' while setting bean property 'sourceList' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#0': Cannot resolve reference to bean 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0' while setting constructor argument with key [1]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0': Cannot resolve reference to bean 'org.springframework.security.authentication.ProviderManager#0' while setting bean property 'authenticationManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.ProviderManager#0': Cannot resolve reference to bean 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authenticationManager': Cannot resolve reference to bean 'customAuthenticationProvider' while setting constructor argument with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customAuthenticationProvider' defined in ServletContext resource [/WEB-INF/spring-security.xml]: Cannot resolve reference to bean 'loginService' while setting bean property 'loginService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'loginService' is defined
苏杰·孟巴拉迪

父上下文不能具有来自子上下文的依赖关系。

在这种情况下,customAuthenticationProvider bean是父上下文的一部分,它与子Web上下文loginService具有依赖关系。

所以你应该

  1. 创建单独的services-context.xml并将loginService bean定义从dispatcher-servlet.xml移到services-context.xml。
  2. contextConfigLocation在web.xml的值列表中添加services-context.xml

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

应用程序上下文。这是什么?

来自分类Dev

从CSS / JS文件内部访问Servlet Web应用程序上下文URL

来自分类Dev

根上下文/应用程序上下文和Servlet上下文

来自分类Dev

Spyne与Flask应用程序上下文

来自分类Dev

Spring root应用程序上下文和servlet上下文混淆

来自分类Dev

从CommandLinerRunner获取应用程序上下文

来自分类Dev

获取应用程序上下文返回null

来自分类Dev

来自应用程序上下文的FragmentManager

来自分类Dev

Android Webview应用程序上下文

来自分类Dev

重新加载或刷新测试方法中的Spring应用程序上下文?

来自分类Dev

Spring递归加载应用程序上下文

来自分类Dev

Spring 4中的差异应用程序上下文

来自分类Dev

在应用程序上下文之外工作-Flask

来自分类Dev

Spring应用程序上下文为null

来自分类Dev

Tizen应用程序上的上下文菜单

来自分类Dev

在单例内部访问应用程序上下文

来自分类Dev

从Flask应用程序中的外部应用程序上下文访问应用程序对象

来自分类Dev

在活动中调用应用程序上下文

来自分类Dev

在Android的存储类中访问应用程序上下文

来自分类Dev

Android中的应用程序上下文

来自分类Dev

将应用程序上下文绑定到其onCreate中是否安全?

来自分类Dev

在应用程序上下文之后加载上下文

来自分类Dev

Spring应用程序-应用程序上下文IOC

来自分类Dev

如何在安全上下文中使用@Autowiring从应用程序上下文中调用bean

来自分类Dev

Spring-请求上下文或仅用于存储访问令牌的安全性上下文?

来自分类Dev

Android获取Android库上下文与应用程序上下文

来自分类Dev

无法在 Spring Boot 应用程序中加载应用程序上下文

来自分类Dev

获取应用程序上下文错误

来自分类Dev

活动上下文与应用程序上下文

Related 相关文章

  1. 1

    应用程序上下文。这是什么?

  2. 2

    从CSS / JS文件内部访问Servlet Web应用程序上下文URL

  3. 3

    根上下文/应用程序上下文和Servlet上下文

  4. 4

    Spyne与Flask应用程序上下文

  5. 5

    Spring root应用程序上下文和servlet上下文混淆

  6. 6

    从CommandLinerRunner获取应用程序上下文

  7. 7

    获取应用程序上下文返回null

  8. 8

    来自应用程序上下文的FragmentManager

  9. 9

    Android Webview应用程序上下文

  10. 10

    重新加载或刷新测试方法中的Spring应用程序上下文?

  11. 11

    Spring递归加载应用程序上下文

  12. 12

    Spring 4中的差异应用程序上下文

  13. 13

    在应用程序上下文之外工作-Flask

  14. 14

    Spring应用程序上下文为null

  15. 15

    Tizen应用程序上的上下文菜单

  16. 16

    在单例内部访问应用程序上下文

  17. 17

    从Flask应用程序中的外部应用程序上下文访问应用程序对象

  18. 18

    在活动中调用应用程序上下文

  19. 19

    在Android的存储类中访问应用程序上下文

  20. 20

    Android中的应用程序上下文

  21. 21

    将应用程序上下文绑定到其onCreate中是否安全?

  22. 22

    在应用程序上下文之后加载上下文

  23. 23

    Spring应用程序-应用程序上下文IOC

  24. 24

    如何在安全上下文中使用@Autowiring从应用程序上下文中调用bean

  25. 25

    Spring-请求上下文或仅用于存储访问令牌的安全性上下文?

  26. 26

    Android获取Android库上下文与应用程序上下文

  27. 27

    无法在 Spring Boot 应用程序中加载应用程序上下文

  28. 28

    获取应用程序上下文错误

  29. 29

    活动上下文与应用程序上下文

热门标签

归档