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

用hasAuthority()测试@PreAuthorize注释

翁硕
2023-03-14

我正在尝试单元测试我的具有@PreAuthorize注释的API。我从智威汤逊得到了认知团体,并将其作为权威。我在api方法的预授权注释中检查了相同的内容。更新:我得到404 No mapping Delete profile/voluc3f5a_test.txt存在

测试类:

import combackend.StorageController;
import com.backend.config.;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;


@RunWith(SpringRunner.class)
@ContextConfiguration(classes = Config.class)
@SpringBootTest
@AutoConfigureMockMvc
//@WebMvcTest(StorageController.class)
public class Tests {


    @Autowired
    private MockMvc mockMvc;

    @Test
    @WithMockUser(username = "admin@domain.com", password = "Password", authorities = "admin")
    public void testDeleteCardProfile() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .delete("http://localhost:9092/profile/{fileKey}", "VOlUc3F5A_test.txt").with(SecurityMockMvcRequestPostProcessors.csrf())).andReturn();
        Assert.assertEquals(200, result.getResponse().getStatus());
        //  .andExpect(status().isOk());


    }
}

控制器方法:

  @ApiResponses({ @ApiResponse(responseCode = "204", description = "Returns the delete success message"),
                @ApiResponse(responseCode = "400", description = "Failed to delete file  : Badrequest"),
                @ApiResponse(responseCode = "404", description = "Failed to delete file, NotFound") })

        @RequestMapping(value = "/profile/{fileKey}", consumes = MediaType.ALL_VALUE, method = { RequestMethod.DELETE })
        @PreAuthorize("hasAuthority('admin')")
        public Mono<Boolean> deleteProfile(@AuthenticationPrincipal Jwt jwt, @PathVariable("fileKey") String fileKey) {
                return storageService.removeProfile(fileKey);
        }

共有1个答案

韩景辉
2023-03-14

错误消息指出this.mockmvcnull。在您共享的代码中,您没有在任何地方初始化this.mockmvc

也许你是想让它变得更好

@Autowired
private MockMvc mockMvc;
 类似资料:
  • 我对spring framework是新手,一直在遵循一些教程/博客来完成这项工作。在执行用户授权部分时,我可以添加@preauthorize(“hasauthority('role_admin')”) 请进入我的问题,我使用的是类型的角色字段,其中包含逗号分隔的值。

  • 我正在使用osgi和基于spring注释的配置。(使用virgo部署应用程序) @preauthorize方法impl上的注释不起作用。 尽管用户没有该角色,但它仍允许用户访问它。 如果我在接口上放置相同的注释,它会为所有用户抛出AccessDenied异常。例外情况: 这是我的配置: 我使用的是spring-security 3.0.4.Release

  • 实际:200 我在MyController中有以下方法: 我创建了以下abstract-security-test.xml:

  • 我想在Spring中测试注入依赖关系。 我想要一个这样的测试: 我尝试过使用ContextConfiguration和一个测试配置文件,但是测试失败了,我不想在测试中使用@autowired,我想创建我的类的一个实例,并且bean是自动autowired的。

  • 是否有任何方法可以重用UserController方法,只需添加不同的安全性预授权,而无需为管理员重写这些方法?

  • 问题内容: 我发现了许多类似的问题,但都没有解决我的问题。我的问题是可以访问的功能 我的spring-security.xml代码如下。 当我添加 我的代码时显示找不到资源错误,并且当我删除我的代码时成功执行但可以访问函数 我的控制器功能是。 问题答案: 你应该有 如果您希望注释起作用。 回答评论: 看来您缺少依赖性。 如果您正在使用Maven,则需要: 如果没有,你可以从这里拿到罐子。