标签: spring-aspects

对 Spring 非托管 bean 的依赖注入

我有一个非托管的 JPA 域类。它是通过new操作符实例化的。

UserAccount account = new UserAccount();
userRepository.save(account)
Run Code Online (Sandbox Code Playgroud)

在我的UserAccount课堂上,我有一个beforeSave()方法依赖于我SecurityService对密码进行哈希编码。

我的问题是“如何让 spring DI 将安全服务注入我的实体?”。似乎 AspectJ 和 LoadTimeWeaving 是我需要的。我已经尝试了一个用于配置的数组,但我似乎无法让它们中的任何一个工作。NullPointerException尝试在注入的对象上调用方法时,我总是得到一个。

UserAccount.java(这是 JPA 实体)

@Entity
@Repository
@Configurable(autowire = Autowire.BY_TYPE)
public class UserAccount implements Serializable {

    @Transient
    @Autowired
    SecurityService securityService;

    private String passwordHash;

    @Transient
    private String password;

    public UserAccount() {
        super();
    }

    @PrePersist
    public void beforeSave() {
        if (password != null) {
            // NullPointerException Here!!!
            passwordHash = securityService.hashPassword(password);  
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

试图表明 spring …

java spring aspectj spring-boot spring-aspects

5
推荐指数
1
解决办法
1万
查看次数

Java AOP JoinPoint不获取参数名称


我正在使用Java Spring Mvc和Spring AOP从用户那里查找参数名称.
我有一个控制器,它从用户获取参数并调用服务.
我有一个方面,在服务之前运行.
方面应检查username和apiKey参数是否存在.
这是我的代码:

控制器:

@RequestMapping(method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String getDomainWithFoundIn(@RequestParam (value="domain") String domain, @RequestParam (value="user") String user, @RequestParam (value="apiKey") String apiKey) throws JsonGenerationException, JsonMappingException, IOException {
    return domainService.getDomainDataWithFoundIn(domain, user, apiKey);
}
Run Code Online (Sandbox Code Playgroud)

域服务接口:

public interface IDomainService {
    public String getDomainDataWithFoundIn(String domainStr, String user, String apiKey);
}
Run Code Online (Sandbox Code Playgroud)

DomainService:

@Override
@ApiAuthentication
public String getDomainDataWithFoundIn(String domainStr, String user, String apiKey) {
//Do stuff here
}
Run Code Online (Sandbox Code Playgroud)

而我的AOP课程:

@Component
@Aspect
public class AuthAspect {
@Before("@annotation(apiAuthentication)") 
public void printIt (JoinPoint …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-aop spring-aspects

3
推荐指数
1
解决办法
9182
查看次数

如何将自定义的DaoAuthenticationProvider加载到Spring Context中?

我在Spring Security中遇到这个问题。

我有一个带有SecurityConfig类的java-config实现,该实现扩展了WebSecurityConfigurerAdapter。

在此类中,我想覆盖方法“ configure()”

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, proxyTargetClass = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter{

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        MyDaoAuthenticationProvider provider = new MyDaoAuthenticationProvider();
        provider.setPasswordEncoder(passwordEncoder());
        provider.setUserDetailsService(securityService);
        auth.authenticationProvider(provider);
    }

    //...

}   
Run Code Online (Sandbox Code Playgroud)

一切都OK,并且有效。

问题是“ MyDaoAuthenticationProvider”组件未在Spring Context上加载。因此,我无法在此类中注入或自动接线任何组件:

public class MyDaoAuthenticationProvider extends DaoAuthenticationProvider {

    @Autowired
    AuthenticationHandler authenticationHandler;    // <- authenticationHandler is null, is not resolved

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        authenticationHandler.authenticate(authentication);    // <- NullPointerException in this point
    }

}
Run Code Online (Sandbox Code Playgroud)

这是AuthenticationHandler类:

@Component …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-security spring-aspects

3
推荐指数
1
解决办法
1万
查看次数

将自定义注释建议应用于 spring data jpa 存储库

我正在研究 mysql 主从复制。我正在使用 spring data jpa(spring boot)。

我需要的是所有写入操作都转到主服务器,而只读操作则均匀分布在多个只读从服务器之间。

为此我需要:

使用特殊的 JDBC 驱动程序:com.mysql.jdbc.ReplicationDriver

在 URL 中设置复制:

spring:
    datasource:
        driverClassName: com.mysql.jdbc.ReplicationDriver
        url: jdbc:mysql:replication://127.0.0.1:3306,127.0.0.1:3307/MyForum?user=root&password=password&autoReconnect=true
        test-on-borrow: true
        validation-query: SELECT 1
    database: MYSQL
Run Code Online (Sandbox Code Playgroud)

需要关闭自动提交。(*) 连接需要设置为只读。

为了确保 JDBC Connection 设置为只读,我创建了一个注释和一个简单的 AOP 拦截器。

注解

package com.xyz.forum.replication;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; 

/**
 * Created by Bhupati Patel on 02/11/15.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ReadOnlyConnection {
}
Run Code Online (Sandbox Code Playgroud)

拦截器

package com.xyz.forum.replication;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; …
Run Code Online (Sandbox Code Playgroud)

spring aspectj spring-aop spring-data-jpa spring-aspects

1
推荐指数
1
解决办法
5088
查看次数