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

无法在现有应用程序中集成spring security

杨飞飙
2023-03-14

我无法在spring security Integration中找到问题。我已经花了2-3天了。所以,请帮助我。

下面是我的web.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>cdl</display-name>
    <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>*.htm</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>startUpServlet</servlet-name>
        <servlet-class>com.qait.cdl.commons.startup.StartUpServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>startUpServlet</servlet-name>
        <url-pattern>/startUpServlet.htm</url-pattern>
    </servlet-mapping>

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

    <context-param>
        <param-name>CDL_ENV</param-name>
        <param-value>staging</param-value>
    </context-param>

    <listener>
        <listener-class>com.qait.cdl.commons.startup.CdlContextListner</listener-class>
    </listener>

    <!-- Session timeout -->
    <session-config>
        <session-timeout>600</session-timeout>
    </session-config>

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

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

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


</web-app>
<?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:context="http://www.springframework.org/schema/context"
    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">

     <import resource="classapth*:spring/SpringSecurityConfig.xml" />
<!--      <bean name="springSecurityFilterChain" class="org.springframework.web.filter.OncePerRequestFilter"/> -->
</beans>     

下面是我的SpringSecurityConfig.xml

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd“>

 <security:http auto-config="true" use-expressions="true">
    <security:intercept-url pattern="/displayAdminPage.htm" access="hasRole('ROLE_ADMIN')" />

    <security:form-login  login-page="/login.htm" authentication-failure-url="/login.htm"/>
    <security:logout logout-url="/logout.htm" logout-success-url="/login.htm"/>
    <security:access-denied-handler error-page="/login.htm" />
</security:http>

<security:authentication-manager>
    <security:authentication-provider user-service-ref="userService" >
    </security:authentication-provider>
</security:authentication-manager>

下面是我的dispatcher-servlet.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:context="http://www.springframework.org/schema/context"
    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">

    <!-- Message resource -->
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>messages</value>
                <value>error</value>
            </list>
        </property>
    </bean>

    <!-- Imports all configuration files -->
    <import resource="classpath*:spring/*.xml" />
    <import resource="classpath*:spring/*/*.xml" />

    <!-- Interceptor mapping -->
    <bean id="handlerMapping"
        class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
        <!-- <property name="interceptors" ref="cdlInterceptor" /> -->
        <property name="interceptors" ref="cdlSessionInterceptor"></property>
    </bean>

    <!-- Tiles view resolver and configuration -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.tiles2.TilesView" />
        <property name="order" value="1" />
    </bean>

    <bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles-defs.xml</value>
            </list>
        </property>
    </bean>

    <!-- XmlView Resolver -->
    <bean class="org.springframework.web.servlet.view.XmlViewResolver">
        <property name="location" value="/WEB-INF/spring-Xmlviews.xml" />
        <property name="order" value="0" />
    </bean>

    <!-- MultipartResolver for file upload -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

    <bean id="rssViewer" class="com.qait.cdl.rssfeed.view.CustomRssViewer" />

    <!-- Default view resolver mapping <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property 
        name="suffix"> <value>.jsp</value> </property> <property name="order" value="1" 
        /> </bean> -->
</beans>

我有以下问题。

  1. 是否有必要在web.xml中给出“filter”标记,如果是,为什么?
  2. 在我的应用程序中,我有两个应用程序上下文(一个用于spring security,另一个用于dispatcher-servlet),那么SpringSecurityConfig.xml是否可以访问dispatcher-servlet.xml中定义的bean定义
  3. spring-security配置的流程是什么。据我所知,我已经理解了intercept-url标记拦截请求并使用表达式语言检查适当的角色。我无法理解通过我提供的authentication-manager在DB中如何显示适当的角色。
    <bean name="userService" class="com.qait.cdl.services.impl.UserServiceImpl">
            <property name="userDao" ref="userDao" />
        </bean> 

下面是userService接口

public interface UserService extends UserDetailsService{
}

此UserDetailsService来自springframework

下面是UserServiceimpl类

public class UserServiceImpl implements UserService {
 public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
        UserDetails userDetails = null;
        if(username != null &&  !"".equals(username)){
            User user = userDao.get(username);
            if(user != null){
                UserGroupAuthority groupAuthority = userDao.getUserAuthority(user);
                if(groupAuthority != null){
                    Collection<GrantedAuthority> grantedAuthorities = getGrantedAuthorities(groupAuthority.getAuthority());
                    userDetails = new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), 
                            true, true, true, true, grantedAuthorities);
                }
            }
        }
        return userDetails;
    }

    @Override
    public Collection<GrantedAuthority> getGrantedAuthorities(String authority) {
        List<GrantedAuthority> grantedAuthorities = new LinkedList<GrantedAuthority>();
        grantedAuthorities.add(new GrantedAuthorityImpl("ROLE_USER"));
        return grantedAuthorities;
    }

    @Override
    public UserGroupAuthority getUserAuthority(User user) {
        return userDao.getUserAuthority(user);
    }
}

共有1个答案

仉宸
2023-03-14

web.xml中激活SpringSecurityFilterChain。这是Spring Security的一个入口。如果SpringSecurityFilterChain被停用,那么Spring Security将永远无法工作。

 类似资料:
  • 问题内容: 我有一个使用Spring Batch和Spring MVC的应用程序。我可以将Spring Batch Admin单独部署,并与我的应用程序使用的数据库一起使用,尽管我想将其集成到我自己的应用程序中,还可能会修改其中一些视图。 有没有简单的方法可以做到这一点,还是我必须将其分叉然后从那里去? 问题答案: 根据这个线程显然有一个简单的方法; 在以下位置为Batch Admin定义Disp

  • 我正在编写基于int-IP:TCPendpoint的客户机-服务器应用程序。用Kotlin写的代码。应用程序包含两个tcp clienst,当它们首先建立了与服务器的连接并进行了一些初始化时,它们应该按照sertain顺序一个接一个地连接到服务器。 作为这种同步的解决方案,我想使用启动依赖tcp客户端的endpoint组。为此,在depended(第二个)客户机中,我将和属性添加到tcp-outb

  • 问题内容: 我在RapidMiner中有一个文本分类过程。它从指定的excel ssheet读取测试数据并进行分类。我还有一个小型Java应用程序,它正在运行此过程。现在,我想在应用程序中添加文件输入部分,以便每次我都可以从应用程序(而不是RapidMiner)中指定excel文件。有什么提示吗? 这是代码: 这是错误: 最好的祝福 Armen 问题答案: 我看到两种方法可以做到这一点。 第一个方

  • 我正在为Android创建一个社交网络集成应用程序。我已经可以在脸书、推特和领英上发帖了。现在我必须在应用程序中集成Flickr。我找不到一个合适的例子如何在Android应用程序中集成flicker。 清单文件: main.xml文件: 我在下面的教程中学习: 代码没有执行,我正在寻找一个解决方案或与Flickr集成的Android示例项目源代码。

  • 我无法为我的Android应用程序创建Facebook登录的哈希键。 根据应用程序文档