2012-08-14 53 views
1

嗨我是Spring Security的新手,但我有一些與Spring一起工作的經驗,我在mkyong.com網站上跟隨教程來創建Spring 3.07中的簡單應用程序,並且它工作正常 但是當我嘗試添加註釋「@Secured(‘ROLE_ADMIN’)」,我發現錯誤「無法創建的LoginController豆」 有誰能夠pointout錯誤或提供給我簡單的彈簧安全應用 這裏是我的代碼Spring Security簡單的應用程序

的web.xml

<web-app id="WebApp_ID" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

    <display-name>Spring MVC Application</display-name> 

    <!-- Spring MVC --> 
    <servlet> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <servlet-class> 
        org.springframework.web.servlet.DispatcherServlet 
       </servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 

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

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

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/mvc-dispatcher-servlet.xml, 
      /WEB-INF/spring-security.xml 
     </param-value> 
    </context-param> 

    <!-- Spring Security --> 
    <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> 

springsecurity.xml(調度程序servlet)

 <beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:s="http://www.springframework.org/schema/security" 
     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.0.xsd"> 


<s:global-method-security secured-annotations="enabled" /> 
<s:http auto-config="true" use-expressions="true"> 
    <s:intercept-url pattern="/index.jsp" access="permitAll" /> 
    <s:intercept-url pattern="/welcome*" access="hasRole('ROLE_USER')" /> 
    <s:intercept-url pattern="/helloadmin*" access="hasRole('ROLE_ADMIN')" /> 
    <s:form-login login-page="/login" default-target-url="/welcome" 
     authentication-failure-url="/loginfailed" /> 
    <s:logout logout-success-url="/logout" /> 
</s:http> 
<bean id="LoginController" class="com.mkyong.common.controller.LoginController" /> 
<s:authentication-manager> 
    <s:authentication-provider> 
    <s:user-service> 
     <s:user name="joseph" password="123456" authorities="ROLE_USER,ROLE_ADMIN" /> 
     <s:user name="jane" password="123456" authorities="ROLE_USER" />   
    </s:user-service> 
    </s:authentication-provider> 
</s:authentication-manager> 
</beans> 

MVC調度servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    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/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <context:component-scan base-package="com.mkyong.common.controller" > 
    </context:component-scan> 
    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix"> 
     <value>/WEB-INF/pages/</value> 
     </property> 
     <property name="suffix"> 
     <value>.jsp</value> 
     </property> 
    </bean> 

</beans> 

logincontroller.java

@Controller 
public class LoginController { 

    @RequestMapping(value = "/welcome", method = RequestMethod.GET) 
    public String printWelcome(ModelMap model, Principal principal) { 

     String name = principal.getName(); 
     model.addAttribute("username", name); 
     model.addAttribute("message", "Spring Security Custom Form example"); 
     return "hello"; 

    } 

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

     return "login"; 

    } 

    // @Secured("ROLE_ADMIN") 
    @RequestMapping(value="/delete", method = RequestMethod.GET) 
    public String DeleteAll(ModelMap model, Principal principal) { 
     return "deleteUsers"; 
    } 

    @RequestMapping(value = "/loginfailed", method = RequestMethod.GET) 
    public String loginerror(ModelMap model) { 

     model.addAttribute("error", "true"); 
     return "login"; 

    } 

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

     return "login"; 

    } 

    } 

代碼在目前的狀態成功地執行,但是當我除去( 'ROLE_ADMIN')代碼所示@Secured的評論錯誤不能創建登錄控制器bean 任何人都可以告訴我缺少什麼或爲我提供簡單的Spring SEcurity應用程序代碼

+0

爲什麼不來與項目本身的示例應用程序啓動? – 2012-08-15 13:40:25

+0

你得到的錯誤的整個堆棧跟蹤是什麼? – 2012-08-16 03:08:17

回答

0

適合我。但我使用3.1.0.RELEASE而不是3.0.7。另外,請檢查您的進口。它應該是導入org.springframework.security.access.annotation.Secured;

+0

感謝您的答覆我會檢查它:) 我已經浪費了我的谷歌搜索這個錯誤的2天 – Jane 2012-08-14 19:35:04

+0

可以分享我你在uploadserver或通過電子郵件創建的項目,我的電子郵件是[email protected] 我仍然倒黴的仍面臨着同樣的錯誤:( – Jane 2012-08-14 20:12:33

+0

我想我還沒有添加正確的jar文件 你能告訴我這瓶需要 – Jane 2012-08-15 17:02:12

0

只需使用這樣的:@Secured({"ROLE_ADMIN"})

+0

沒工作過同樣的錯誤 我想我還沒有添加正確的jar文件 你能告訴我哪罐子是必需的 – Jane 2012-08-15 17:01:04

+0

爲我們提供錯誤日誌,以便我可以告訴你需要什麼'.jar'文件。 – 2012-08-16 04:56:24

相關問題