当前位置: 首页 > 知识库问答 >
问题:

委托方法安全性元数据错误

苏乐
2023-03-14

我已经使用spring框架构建了一个web项目,并据此向其中添加了jar。但是当我执行它时,它会出现以下错误。请为我提供此错误的解决方案。

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginController' defined in file [D:\WebsiteDemo\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\loginexample\WEB-INF\classes\com\login\controller\LoginController.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.methodSecurityMetadataSourceAdvisor': Cannot resolve reference to bean 'org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource#0' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource#0': 1 constructor arguments specified but no matching constructor found in bean 'org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource#0' (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)

我的调度文件是

  <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:security="http://www.springframework.org/schema/security"
       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/aop http://www.springframework.org/schema/aop/spring-aop-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
       http://www.springframework.org/schema/security
       http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<context:component-scan base-package="com.login.controller"/>
<mvc:annotation-driven />

<security:global-method-security secured-annotations="enabled"/>

<security:http auto-config="true">
<security:intercept-url pattern="/main*" access="ROLE_ADMIN,ROLE_REGULAR_USER" />
<security:form-login login-page="/login" default-target-url="/main"
authentication-failure-url="/loginError"/>
</security:http>

<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="alpha" password="pass1" authorities="ROLE_ADMIN" />
<security:user name="beta" password="pass2" authorities="ROLE_REGULAR_USER" />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>

LoginController代码

package com.login.controller;

import java.security.Principal;

import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class LoginController {

    @RequestMapping(value="/main", method = RequestMethod.GET)
    public String printWelcome(ModelMap model, Principal principal ) {
        String name = principal.getName();
        model.addAttribute("username", name);
        return "main_page";
        }

    @RequestMapping(value="/login", method = RequestMethod.GET)
    public String login(ModelMap model) { 
        return "login_page"; 
    }

    @RequestMapping(value="/loginError", method = RequestMethod.GET)
        public String loginerror(ModelMap model) {
        model.addAttribute("error", "true");
        return "login_page";
    }


    @Secured({"ROLE_REGULAR_USER","ROLE_ADMIN"})
    @RequestMapping(value="/common", method = RequestMethod.GET)
    public String common(ModelMap model) {
        return "common_page";
    }

    @RequestMapping(value="/logout", method = RequestMethod.GET)
    public String logout(ModelMap model) {
        return login(model); 
    }
}

共有1个答案

韩智敏
2023-03-14

不要通过组件扫描获取LoginController,而是在bean定义xml(您称之为dispatcher文件)中定义它:

 <?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:security="http://www.springframework.org/schema/security"
       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/aop http://www.springframework.org/schema/aop/spring-aop-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
       http://www.springframework.org/schema/security
       http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<mvc:annotation-driven />

<!-- Controller bean definitions -->

<bean id="loginController" class="com.login.controller.LoginController" />

<security:global-method-security secured-annotations="enabled"/>

<security:http auto-config="true">
<security:intercept-url pattern="/main*" access="ROLE_ADMIN,ROLE_REGULAR_USER" />
<security:form-login login-page="/login" default-target-url="/main"
authentication-failure-url="/loginError"/>
</security:http>

<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="alpha" password="pass1" authorities="ROLE_ADMIN" />
<security:user name="beta" password="pass2" authorities="ROLE_REGULAR_USER" />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

</beans>
 类似资料:
  • 在linux上执行以下convert命令时,字体出现委托错误。 转换:委托库支持不内置'/usr/share/fonts/type1/gsfonts/n021003l.pfb'(Freetype)@警告/注释. c/RenderFreetype/1818。转换:没有此图像格式的解码委托PNG错误/成分. c/ReadImage/509。转换:委托库支持不内置'/usr/share/fonts/ty

  • 主要内容:类委托,属性委托,标准委托,可观察属性 Observable,把属性储存在映射中,Not Null,局部委托属性,属性委托要求,翻译规则,提供委托委托模式是软件设计模式中的一项基本技巧。在委托模式中,有两个对象参与处理同一个请求,接受请求的对象将请求委托给另一个对象来处理。 Kotlin 直接支持委托模式,更加优雅,简洁。Kotlin 通过关键字 by 实现委托。 类委托 类的委托即一个类中定义的方法实际是调用另一个类的对象的方法来实现的。 以下实例中派生类 Derived 继承了接口

  • 主要内容:声明委托,实例化委托,多播委托(合并委托)C# 中的委托(Delegate)类似于 C 或 C++ 中的函数指针,是一种引用类型,表示对具有特定参数列表和返回类型的方法的引用。委托特别适用于实现事件和回调方法,所有的委托都派生自 System.Delegate 类。在实例化委托时,可以将委托的实例与具有相同返回值类型的方法相关联,这样就可以通过委托来调用方法。另外,使用委托还可以将方法作为参数传递给其他方法, 委托具有以下特点: 委托类似

  • 关于“行为委派”的良好讨论可以在 找到。

  • 委托模式是软件设计模式中的一项基本技巧。在委托模式中,有两个对象参与处理同一个请求,接受请求的对象将请求委托给另一个对象来处理。 Kotlin 直接支持委托模式,更加优雅,简洁。Kotlin 通过关键字 by 实现委托。 类委托 类的委托即一个类中定义的方法实际是调用另一个类的对象的方法来实现的。 以下实例中派生类 Derived 继承了接口 Base 所有方法,并且委托一个传入的 Base 类的

  • 本文向大家介绍PHP __call()方法实现委托示例,包括了PHP __call()方法实现委托示例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了PHP __call()方法实现委托。分享给大家供大家参考,具体如下: 委托是指一个对象转发或者委托一个请求给另一个对象,被委托的一方替原先对象处理请求。这类似于继承,和在子类中调用父类的方法有点儿相似。 但在继承时,父类与子类的关系是固定的