2013-03-05 3 views
1

웹 응용 프로그램은 데이터베이스 기반 인증 및 권한 부여에 스프링 보안을 사용하고 있습니다. 이 같은를 구성한 : 이 내가 applicationContext.xml과 applicationContext-을 도입 내 web.xml 파일입니다 security.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:sec="http://www.springframework.org/schema/security" 
     xmlns:beans="http://www.springframework.org/schema/mvc" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> 
     <property name="url" value="jdbc:oracle:thin:@10.60.2.4:1521:agribase"/> 
     <property name="username" value="sabka"/> 
     <property name="password" value="sabka"/> 
    </bean> 

    <sec:http use-expressions="true"> 
     <sec:intercept-url pattern="/secured/**" access="ROLE_USER"/> 
     <sec:intercept-url pattern="/admin/*" access="ROLE_ADMIN"/> 
     <sec:form-login login-page="/jsp/homePage.jsp" 
         authentication-failure-url="/jsp/homePage.jsp" 
         default-target-url="/jsp/homePage.jsp"/> 
    </sec:http> 

    <sec:authentication-manager> 
     <sec:authentication-provider> 
      <sec:jdbc-user-service 
       data-source-ref="dataSource" 
       users-by-username-query=" 
        SELECT username,password, enabled FROM users WHERE username=?" 
       authorities-by-username-query=" 
        SELECT u.username, ur.authority 
        FROM users u, user_roles ur WHERE u.user_id = ur.user_id AND u.username=?"/> 
     </sec:authentication-provider> 
    </sec:authentication-manager> 
</beans> 

이 내 applicationContextSecurity.xml이

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 
     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
     id="WebApp_ID" version="2.5"> 

    <display-name>Struts2Example14</display-name> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/applicationContext.xml 
      /WEB-INF/applicationContext-security.xml 
     </param-value> 
    </context-param> 

    <filter> 
     <filter-name>struts2</filter-name> 
     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
    </filter> 

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

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

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

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

입니다 나는 그것이 일식 실행이 오류가 나타납니다

,691 :

Unexpected exception parsing XML document from ServletContext resource [/WEB- INF/applicationContext-security.xml]; 

nested exception is org.springframework.beans.BeanInstantiationException:Could not instantiate bean class 
[org.springframework.security.config.SecurityNamespaceHandler]:Constructor threw exception; 

nested exception is java.lang.NoClassDefFoundError: org/springframework/aop/config/AbstractInterceptorDrivenBeanDefinitionDecorator 

이 내 클래스 경로입니다 363,210 는

<?xml version="1.0" encoding="UTF-8"?> 
<classpath> 
<classpathentry kind="src" path="src"/> 
<classpathentry kind="src" path="WebContent/WEB-INF"/> 
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.St andardVMType/jre6"> 
<attributes> 
<attribute name="owner.project.facets" value="java"/> 
</attributes> 
</classpathentry> 
<classpathentry exported="true" kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v6.0"> 
<attributes> 
<attribute name="owner.project.facets" value="jst.web"/> 
</attributes> 
</classpathentry> 
<classpathentry exported="true" kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/> 
<classpathentry exported="true" kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/> 
<classpathentry exported="true" kind="lib" path="//10.60.2.7/Sabka/lib/ojdbc14.jar"/> 
<classpathentry exported="true" kind="lib" path="//10.60.2.7/Sabka/lib/ojdbc14dms.jar"/> 
<classpathentry exported="true" kind="lib" path="D:/SpringWS/SpringStruts2/WebContent/WEB-INF/lib/antlr-runtime-3.0.jar"/> 
<classpathentry exported="true" kind="lib" path="D:/SpringWS/SpringStruts2/WebContent/WEB-INF/lib/commons-fileupload-1.2.1.jar"/> 
<classpathentry exported="true" kind="lib" path="D:/SpringWS/SpringStruts2/WebContent/WEB-INF/lib/commons-io-1.3.2.jar"/> 
<classpathentry exported="true" kind="lib" path="D:/SpringWS/SpringStruts2/WebContent/WEB-INF/lib/commons-logging-1.1.jar"/> 
<classpathentry exported="true" kind="lib" path="D:/SpringWS/SpringStruts2/WebContent/WEB-INF/lib/freemarker-2.3.13.jar"/> 
<classpathentry exported="true" kind="lib" path="D:/SpringWS/SpringStruts2/WebContent/WEB-INF/lib/junit-3.8.1.jar"/> 
<classpathentry exported="true" kind="lib" path="D:/SpringWS/SpringStruts2/WebContent/WEB-INF/lib/ognl-2.6.11.jar"/> 
<classpathentry exported="true" kind="lib" path="D:/SpringWS/SpringStruts2/WebContent/WEB-INF/lib/org.springframework.asm-3.0.0.M3.jar"/> 
<classpathentry exported="true" kind="lib" path="D:/SpringWS/SpringStruts2/WebContent/WEB-INF/lib/org.springframework.beans-3.0.0.M3.jar"/> 
<classpathentry exported="true" kind="lib" path="D:/SpringWS/SpringStruts2/WebContent/WEB-INF/lib/org.springframework.context-3.0.0.M3.jar"/> 
<classpathentry exported="true" kind="lib" path="D:/SpringWS/SpringStruts2/WebContent/WEB-INF/lib/org.springframework.core-3.0.0.M3.jar"/> 

+0

도움이 될 것입니다. http://forum.springsource.org/showthread.php?59938-Custom-implementation-of-Spring-Security-s-UserDetailsService – nav0611

답변

1

당신은 클래스 패스에 스프링 AOP 항아리-파일을 포함나요?

+0

아니요, 그래서 나는 jop 파일을 가지고 있지 않다. –

+1

스프링 보안은 AOP를 사용하고 있기 때문에 spring-aop jar 파일을 추가 해보자. 그건 당신의 현재 문제를 해결해야합니다. –

+0

그 문제가 해결되었습니다, 지금 이것은 문제입니다 : 예기치 않은 예외 ServletContext 자원 [/WEB-INF/applicationContext-security.xml]에서 XML 문서를 구문 분석; 중첩 예외는 java.lang.NoSuchMethodError입니다 : org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.getLocalName (Lorg/w3c/dom/Node;) Ljava/lang/String; –

3

이 예제는 Spring 3.2와 Spring Security 3.1 및 Hibernate 4.1.9의 사용법을 보여줍니다.

은 pom.xml 파일 web.xml에

 <!-- spring dependency--> 
      <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context-support</artifactId> 
      <version>${spring.framework.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-orm</artifactId> 
      <version>${spring.framework.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-core</artifactId> 
      <version>${spring.framework.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>${spring.framework.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>${spring.framework.version}</version> 
     </dependency> 
     <!-- START: Spring security --> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-core</artifactId> 
      <version>${spring.security.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-config</artifactId> 
      <version>${spring.security.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-web</artifactId> 
      <version>${spring.security.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-taglibs</artifactId> 
      <version>${spring.security.version}</version> 
     </dependency> 
     <!-- END: Spring security --> 

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

     <!-- START: 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> 
     <!-- END: Spring Security --> 

스프링 security.xml mvc.xml에서

<http auto-config="true" access-denied-page="/denied"> 
     <intercept-url pattern="/role1/*" access="ROLE1" /> 
     <intercept-url pattern="/role2/*" access="ROLE2" /> 
     <intercept-url pattern="/role3/*" access="ROLE3" /> 
     <intercept-url pattern="/home" access="ROLE1,ROLE2,ROLE3"/>   
     <form-login login-page="/login" default-target-url="/home" 
      authentication-failure-url="/loginfailed" /> 
      <logout 
      invalidate-session="true" 
      delete-cookies="SPRING_SECURITY_REMEMBER_ME_COOKIE" 
      logout-success-url="/index"></logout> 
    </http> 
<beans:bean class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler"/> 
    <authentication-manager > 
    <authentication-provider ref="authProvider"></authentication-provider> 
    </authentication-manager> 
<beans:bean id="authProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider"> 
    <beans:property name="userDetailsService" ref="userDetailsServiceImpl" /> 
    <beans:property name="passwordEncoder" ref="encoder" /> 
</beans:bean> 
<!-- For hashing and salting user passwords --> 
    <beans:bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/> 

에 다음과 같은 의존성을 포함

<security:global-method-security secured-annotations="enabled"/> 
    <bean id="userDetailsServiceImpl" class="com.service.UserDetailsServiceImpl"> 
     <property name="sessionFactory" ref="sessionFactory"></property> 
    </bean> 
@Service 
@Transactional(readOnly = true) 
public class UserDetailsServiceImpl implements UserDetailsService { 


    UserDao userDAO = new UserDaoImpl(); 

    public UserDetails loadUserByUsername(String userName) 
      throws UsernameNotFoundException { 

     Users domainUser = userDAO.getUser(userName); 
     Roles role = domainUser.getRoles(); 
     boolean enabled = true; 
     boolean accountNonExpired = true; 
     boolean credentialsNonExpired = true; 
     boolean accountNonLocked = true; 

     return new User(domainUser.getUsername(), 
       domainUser.getUsersPassword(), enabled, accountNonExpired, 
       credentialsNonExpired, accountNonLocked, 
       getAuthorities(role.getRoleName())); 
    } 

    public Collection<? extends GrantedAuthority> getAuthorities(String role) { 
     List<GrantedAuthority> authList = getGrantedAuthorities(getRoles(role)); 
     return authList; 
    } 

    public List<String> getRoles(String role) { 

     List<String> roles = new ArrayList<String>(); 
     if ("ROLE1".equals(role)) { 
      roles.add("ROLE1"); 
     } else if ("ROLE2".equals(role)) { 
      roles.add("ROLE2"); 
     } 
     else if ("ROLE3".equals(role)) { 
      roles.add("ROLE3"); 
     } 
     return roles; 
    } 

    public static List<GrantedAuthority> getGrantedAuthorities(
      List<String> roles) { 
     List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); 

     for (String role : roles) { 
      authorities.add(new SimpleGrantedAuthority(role)); 
     } 
     return authorities; 
    } 

UserDetailsServiceImpl

에서

이제 당신은 이름에 따라 사용자의 POJO를 얻기 위해 DAO에서 하나의 방법을 확인해야합니다. 그러면 잘 작동합니다.

+0

다음과 같은 보안 구성을 사용하려고했습니다 : gkbstar

+0

그러나 그 오류를 던지고 : 발생 원인 : org.springframework.beans.NotWritablePropertyException : bean 클래스 [com.ipms.service.impl.CustomUserDetailsService]의 'passwordEncoder'속성이 올바르지 않습니다. Bean 속성 'passwordEncoder'가 있습니다. 쓸 수 없거나 setter 메소드가 잘못되었습니다. setter의 매개 변수 유형이 getter의 반환 유형과 일치합니까? – gkbstar

관련 문제