@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface MockAuthorize {
String[] roles() default {Role.ROLE_ADMIN};
String username() default "testUser";
}
在研究了如何在自定义注释方法之前和之后调用代码之后,我决定为此使用Spring AOP。
@Aspect
public class MockAuthorizeAspect {
@Around("app.annotation.AspectJPointcutStorage.mockAuthorize()")
public Object loginBeforeAndLogoutAfter(ProceedingJoinPoint joinPoint, MockAuthorize mockAuthorize) throws Throwable {
final String[] roles = mockAuthorize.roles();
final String username = mockAuthorize.username();
Set<GrantedAuthority> resolvedRoles = Sets.newLinkedHashSet();
for (String role : roles) {
resolvedRoles.add(new SimpleGrantedAuthority(role));
}
SecurityContextHolder.getContext().setAuthentication(new PreAuthenticatedAuthenticationToken(username, "", resolvedRoles));
final Object proceed = joinPoint.proceed();
AuthentificationService.AuthentificationMock.logout();
return proceed;
}
}
所以我把Config添加到我的pom.xml中
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
但当我在调试模式下执行测试时...
@SpringBootTest
class UserTest {
@Autowired
private UserService userService;
@Autowired
private RoleService roleService;
@Autowired
private MockAuthorizeAspect mockAuthorizeAspect;
private User user = null;
@BeforeEach
void setUp() {
try {
AuthentificationService.AuthentificationMock.loginAsAdminOnly();
user = new User("hansmeier", "password", "Hans", "Meier", "hansimeier@gmx.net", "+4915465656", null);
user.setRoles(Sets.newHashSet(roleService.getByName(Role.ROLE_USER).orElse(null)));
AuthentificationService.AuthentificationMock.logout();
} catch (Exception e) {
//Nothing to do here!
}
}
@AfterEach
void tearDown() {
try {
AuthentificationService.AuthentificationMock.loginAsAdminOnly();
userService.delete(user);
AuthentificationService.AuthentificationMock.logout();
} catch (Exception e) {
//Nothing to do here!
}
}
@Test
@SneakyThrows
void testCreate() {
//GIVEN
AuthentificationService.AuthentificationMock.loginAsAdminOnly();
//WHEN
user = userService.save(user);
//THEN
assertEquals(userService.getByUsernameResolved(user.getUsername()).orElse(null), user);
AuthentificationService.AuthentificationMock.logout();
}
@Test
void testCreateUnauthorized() {
boolean failed = false;
//WHEN
try {
user = userService.save(user);
} catch (AuthenticationCredentialsNotFoundException e) {
failed = true;
}
assertTrue(failed);
}
@Test
@SneakyThrows
void testGetByUsername() {
//GIVEN
AuthentificationService.AuthentificationMock.loginAsAdminOnly();
user = userService.save(user);
//WHEN
final User foundUser = userService.getByUsernameResolved(user.getUsername()).orElse(null);
AuthentificationService.AuthentificationMock.logout();
//THEN
assertEquals(user, foundUser);
}
@Test
@MockAuthorize
void testGetById() {
//GIVEN
user = userService.save(user);
//WHEN
final User foundUser = userService.getById(user.getId()).orElse(null);
//THEN
assertEquals(user, foundUser);
}
@Test
@MockAuthorize
void testGetAll() {
//GIVEN
user = userService.save(user);
//WHEN
final Set<User> allResolved = userService.getAllResolved();
//THEN
for (User u : allResolved) {
if (u.equals(user)) {
assertTrue(true);
return;
}
}
fail("User is not in all Users, something went wrong");
}
}
@Configuration
@EnableAspectJAutoProxy
public class AOPConfiguration {
@Bean
public MockAuthorizeAspect mockAuthorizeAspect(){
return new MockAuthorizeAspect();
}
}
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.1-jre</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>16.0.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
来自文档
==Spring AOP功能和目标
Spring AOP目前只支持方法执行连接点(建议在Spring bean上执行方法)
@Around("@annotation(app.annotation.AspectJPointcutStorage.MockAuthorize)")
我正在编写一个库/sdk,它可以拦截任何使用自定义注释进行注释的方法。代码的工作方式有点像这样 截取这个的方面有一个切入点表达式 当我在与相同的包中描述方面时,此代码工作正常。但是,如果我创建一个单独的库并定义方面,因为它无法拦截。有帮助吗? 回应@Bond的评论 Spring版本:Spring上下文-4.1.7。发布aspectj-1.6.5问题的关键是注释不会在同一个项目中使用。在编译之后,它
在我的Spring Boot项目中,我创建了一个自定义注释,其中validator扩展了ConstraintValidator,以验证RequestBody中的一些字段。注释对于非嵌套字段可以很好地工作,但对于嵌套字段不调用验证器。 我的注释如下所示: 我的验证类: 它在这样的情况下工作正常: 但是当放在嵌套对象上时,不会调用验证器: 类在我的中的用法: 关于如何解决这个问题有什么想法吗?我已经尝
我有这个接口: 接口的这个实现:
我在网上搜索了一个关于如何使用Spring AOP调用自定义方法注释的清晰示例,但没有找到一个清晰的示例。 我正在构建一个框架,以便在调用任何POJO上的某些方法时在上下文中注入用户配置文件。 框架API应该通过自定义方法注释调用,例如。我可以构建注释部分和解析器,我的问题是在调用带注释的方法时调用我的解析器的最佳方式是什么。 我们正在使用Spring 3.0,想知道配置Spring框架以理解那些
本文向大家介绍你知道自定义事件吗?jQuery里的fire函数是什么意思,什么时候用?相关面试题,主要包含被问及你知道自定义事件吗?jQuery里的fire函数是什么意思,什么时候用?时的应答技巧和注意事项,需要的朋友参考一下 [jQuery] 你知道自定义事件吗?jQuery里的fire函数是什么意思,什么时候用?
我发现了几个与此相关的(不是重复的)问题,但它们不能让我满意。 我无法理解在哪里以及为什么要使用? 我在一本书中读到了一个自定义注释的示例,但没有详细解释。 myMeth()内的输出与预期一致。 关于这个例子,我有几个问题。 1-如何在此程序中使用和?或