Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring-Security@Preauthorize标记在单元测试中不起作用_Spring Security - Fatal编程技术网

Spring-Security@Preauthorize标记在单元测试中不起作用

Spring-Security@Preauthorize标记在单元测试中不起作用,spring-security,Spring Security,(首先,我很抱歉,我的代码只能有一级缩进) 我正试图编写一个单元测试来测试我的服务层方法。这些服务类的接口用@Preauthorize注释: public interface LocationService { void setLocationRepository(LocationRepository locationRepository); /** * Get all Location objects from the backend repository

(首先,我很抱歉,我的代码只能有一级缩进)

我正试图编写一个单元测试来测试我的服务层方法。这些服务类的接口用@Preauthorize注释:

public interface LocationService {

    void setLocationRepository(LocationRepository locationRepository);

    /**
     * Get all Location objects from the backend repository
     * @return
     */

    @PreAuthorize("has_role('ROLE_ADMIN')")
    List<Location> getAll();
公共接口定位服务{
void setLocationRepository(LocationRepository LocationRepository);
/**
*从后端存储库获取所有位置对象
*@返回
*/
@预授权(“具有角色('role\u ADMIN'))
List getAll();
单元测试如下所示:

@Before
public void setUp() {
    admin = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken("admin", "admin"));
    user = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken("user", "user"));
    // create Mock Repository
    // set up the actual service WITH the repository
    locationService = new LocationServiceImpl();
    locationService.setLocationRepository(locationRepository);
}

@Test(expected = AccessDeniedException.class)
@SuppressWarnings("unused")
public void testGetAllAsUser() {
    SecurityContextHolder.getContext().setAuthentication(user);
    List<Location> resultList = locationService.getAll();
}
@之前
公共作废设置(){
admin=authenticationManager.authenticate(新用户名passwordauthenticationtoken(“admin”、“admin”));
user=authenticationManager.authenticate(新用户名PasswordAuthenticationToken(“用户”、“用户”));
//创建模拟存储库
//使用存储库设置实际服务
locationService=新的LocationServiceImpl();
locationService.setLocationRepository(locationRepository);
}
@测试(预期=AccessDeniedException.class)
@抑制警告(“未使用”)
public void testGetAllAsUser(){
SecurityContextHolder.getContext().setAuthentication(用户);
List resultList=locationService.getAll();
}
最后,这里是my applicationContext.xml中的安全上下文:

<!-- Temporary security config. This will get moved to a separate context 
    file, but I need it for unit testing right now -->
<security:http use-expressions="true">
    <security:form-login />
    <security:session-management
        invalid-session-url="/timeout.jsp">
        <security:concurrency-control
            max-sessions="1" error-if-maximum-exceeded="true" />
    </security:session-management>
</security:http>
<security:authentication-manager alias="authenticationManager">
    <security:authentication-provider>
        <security:password-encoder hash="plaintext" />
        <security:user-service>
            <security:user name="admin" password="admin"
                authorities="ROLE_ADMIN" />
            <security:user name="user" password="user"
                authorities="ROLE_USER" />
        </security:user-service>
    </security:authentication-provider>
</security:authentication-manager>

<security:global-method-security
    pre-post-annotations="enabled" proxy-target-class="true" />

不幸的是,@PreAuthorize标记被忽略,允许角色为_USER的人运行getAll()

有人能帮忙吗

杰森

  • 您是否正在使用spring junit runner运行单元测试
  • 您是否指向该单元测试的正确spring配置文件
  • 您是否为安全方面配置了加载时编织
该行:

locationService = new LocationServiceImpl();
创建一个新的位置服务,完全绕过spring。如果您使用的是spring junit runner,那么您应该使用@Resource来注入locationService,以便您使用的是spring bean,而不仅仅是pojo。

  • 您是否正在使用spring junit runner运行单元测试
  • 您是否指向该单元测试的正确spring配置文件
  • 您是否为安全方面配置了加载时编织
该行:

locationService = new LocationServiceImpl();

创建一个新的位置服务,完全绕过spring。如果您使用的是spring junit runner,那么您应该使用@Resource来注入locationService,这样您使用的是spring bean,而不仅仅是pojo。

好的一点,我将修复它,直接从上下文中获取locationService bean。我还将尝试找出f我已经设置了编织,但是代码正在工作,所以我要到早上才能检查它。你不需要启用LTW来工作,但是如果没有,那么你需要确保你是在spring代理上调用方法,而不是直接在impl上调用方法,这就是我回答的后半部分。好的,你是ri嗯。我犯了一个非常愚蠢的错误。我转过身,让Spring自动连线locationService。这正是我所学到的,这是一个我不会很快忘记的教训。很高兴它起到了作用。嗨,我正在调用服务的方法,并将服务类包含在@ContextConfiguration中。但是它对我不起作用,我正在使用SpringRunner。什么我会让Spring为服务接口使用AOP代理,该服务接口在单元测试中具有预授权注释。好的方面,我会修复它以直接从上下文获取LocationService bean。我还会尝试确定我是否已经设置了编织,但代码正在工作,所以我要到早上才能检查它。您不需要e LTW已启用以使其工作,但如果不是,则需要确保在spring代理上调用该方法,而不是直接在impl上调用该方法,这就是我回答的后半部分。好的,你是对的。我犯了一个非常愚蠢的错误。我转过身,让spring自动连接locationService。这正是h我学到了什么,这是一个我不会很快忘记的教训。很高兴它起到了作用。嗨,我正在调用服务的方法,并将服务类包含在@ContextConfiguration中。但是它对我不起作用,我正在使用SpringRunner。是什么导致Spring对在t中具有预授权注释的服务接口使用AOP代理他进行了单元测试。