委派方法安全元数据错误

时间:2015-10-07 12:08:52

标签: java spring-mvc

我使用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 个答案:

答案 0 :(得分:0)

不要选择LoginController组件扫描,而是在bean定义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: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>