简体   繁体   English

如何模拟Spring SecurityContext,以便可以与TestNG一起使用?

[英]How to mock Spring SecurityContext so I can use it with TestNG?

I have to build a Unit test to test some user actions, when they are authenticated. 我必须建立一个单元测试来测试一些用户操作,当它们通过身份验证时。

I have everything in place with EasyMock and TestNG. 我拥有EasyMock和TestNG的所有功能。

But I cannot find a way to inject a SecurityContextHolderStrategy (I am using this interface in order to be able to inject and mock SecurityContextHolder in my Controller so I can have 2 different setups one for production and one for testing using separated applicationContext.xml) 但是我找不到一种注入SecurityContextHolderStrategy的方法(我正在使用此接口,以便能够在我的控制器中注入和模拟SecurityContextHolder,因此我可以有2种不同的设置用于生产,一种用于使用分离的applicationContext.xml进行测试)

But I am having a hard time creating a bean that can match SecurityContextHolderStrategy with the proper settings ( in Test a empty context and in prod inject the real one). 但是我很难创建一个可以将SecurityContextHolderStrategy与正确的设置匹配的bean(在Test一个空上下文中,在prod中注入真实的bean)。

Can anyone help me ? 谁能帮我 ?

here is a code sample of the Controller. 这是控制器的代码示例。

@Controller
@RequestMapping("/topic/**")
public class TopicController {

@Autowired
PostRepository postRepo;
@Autowired
TopicRepository top;
@Autowired
PersonRepository per;
@Autowired
ProductRepository pro;
@Autowired
TopicControllerHelper topHelper;
@Autowired
SecurityContextHolderStrategy securityContext;

@RequestMapping(value="/topic/{topicId}", method=RequestMethod.GET)
public ModelAndView showPage(@PathVariable("topicId") int id){
    ModelAndView model = new ModelAndView("/topic");
    Topic topic = top.findTopicByID((long) id);
    model.addObject("topic",topic); 
    Post post = new Post();
    post.setPerson(topic.getPerson());
    post.setTopic(topic);
    model.addObject("post",post);
    model.addObject("logged",securityContext.getContext().getAuthentication().getName());
    return model;
}

And my testApplicationContext.xml 还有我的testApplicationContext.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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:sec="http://www.springframework.org/schema/security" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <context:spring-configured />
    <context:component-scan base-package="br.com.gsc" />
    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<!--    <tx:annotation-driven transaction-manager="transactionManager"/>     -->
    <mvc:annotation-driven />

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="gscTest" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean id="securityContext" class="org.springframework.security.core.context.SecurityContextHolderStrategy">

I am lost at here !!! 我在这里迷路了! WHAT should be here in Test and Production to make the Context work in each .xml ? 为了使Context在每个.xml中都能工作,测试和生产中应该包含什么内容?

    </bean>

    <!-- <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" 
        p:definitions="/WEB-INF/tiles-defs.xml" /> -->

</beans>

Create a separate bean with interface that you can mock and let that bean read the Authentication from the static context. 创建一个具有可以模拟的接口的单独的Bean,并让该Bean从静态上下文读取Authentication。 If you have a user object, it could even return a user rather than a name. 如果您有一个用户对象,它甚至可以返回用户而不是名称。

我意识到这不是您问题的直接答案,但是您是否考虑过使用Powermock模拟静态SecurityContextHolder.getSecurityContext()方法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM