Get current user with spring aop - spring-aop

I use spring boot 2 and spring security.
With aop, I search to get user who called the method.
#Aspect
#Component
public class LogAspect {
#Before("execution(* com.moplogy.controller.*))")
public void getAllAdvice(JoinPoint joinPoint){
System.out.println("method called " + joinPoint.getSignature().getName());
}
}
any idea?

try to add in you method this line
Authentication auth = SecurityContextHolder.getContext().getAuthentication();

Related

Is it possible to fetch data from database before server is getting started?

I am working on a Spring project. I want to use scheduler in it and want to schedule it on a variable date. This date has to be taken from database. Is it possible to fetch data from database before server is getting started?
Two solutions come to my mind:
#PostConstruct annotated method of some #Component:
#Component
public class MyBean
{
#PostConstruct
public void init()
{
// Blocking DB call could go here
}
}
Application Events. For the ApplicationReadyEvent:
#Component
public class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent>
{
#Override
public void onApplicationEvent(ApplicationReadyEvent event)
{
// DB call could go here
//
// Notice that if this is a web services application, it
// would potentially be serving requests while this method
// is being executed
}
}

Spring aop is not getting triggerd for external jar method in spring boot application

I am trying to have point cut for a method in jar and it is not getting triggered properly
I have my rest endpoint code as below :
package com.example.log.javatpoint;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class Test{
#Autowired
Operation op;
private static final Logger LOG = LogManager.getLogger(Test.class);
#RequestMapping(value = "/customs", method = RequestMethod.GET)
public String custom() {
op.msg();
op.display();
LOG.info("Hello World");
LOG.info("Hello {0}", "World 2");
return "custom";
}
}
Operation class :
#Component
public class Operation{
public void msg(){System.out.println("msg() is invoked");}
public void display(){System.out.println("display() is invoked");}
}
#Aspect
#Component
public class TrackOperation
{
#Pointcut("execution(* Operation.*(..))")
public void abcPointcut(){}
#Around("abcPointcut()")
public Object myAdvice(ProceedingJoinPoint pjp) throws Throwable
{
System.out.println("Additional Concern Before calling actual method");
Object obj=pjp.proceed();
System.out.println("Additional Concern After calling actual method");
return obj;
}
#Pointcut("execution(* org.apache.logging.log4j.LogManager.info(..))")
public void abcPointcutLog(){}
#Around("abcPointcutLog()")
public Object myAdviceLog(ProceedingJoinPoint pjp) throws Throwable
{
System.out.println("Additional Concern Before calling actual method");
Object obj=pjp.proceed();
System.out.println("Additional Concern After calling actual method");
return obj;
}
}
Note : Point cut is working for Operation class where as point cut is not working for org.apache.logging.log4j.LogManager tried also providing org.apache.logging.log4j.Logger in point cut.
I expect the output as :
Additional Concern Before calling actual method
2019-09-24 12:28:58.540 INFO 10076 --- [nio-8080-exec-1] com.example.log.javatpoint.Test : Hello World
Additional Concern After calling actual method
Additional Concern Before calling actual method
2019-09-24 12:28:58.540 INFO 10076 --- [nio-8080-exec-1] com.example.log.javatpoint.Test : Hello {0}
Additional Concern After calling actual method
but the actual output is :
2019-09-24 12:28:58.540 INFO 10076 --- [nio-8080-exec-1] com.example.log.javatpoint.Test : Hello World
2019-09-24 12:28:58.540 INFO 10076 --- [nio-8080-exec-1] com.example.log.javatpoint.Test : Hello {0}
This question is a "classic" and has been asked so many times here already...
Please read the Spring AOP manual before using a tool you don't know. It will tell you that Spring AOP can only be applied to Spring components/beans, not to non-Spring POJOs. For that you need full AspectJ which you can use within Spring or completely without Spring.
Log4J classes are not Spring components, so the above applies to your situation. Here you find information about how to use AspectJ load-time weaving (LTW) instead of Spring AOP for your purpose.

Spring data JPA waiting find

I have some method which import prices for airports and save it to database through Spring data JPA repositories.
Import method is this:
#Transactional
public Future<Boolean> importFuel(File serverFile, Long providerIdLong) {
final FuelProvider fuelProvider = fuelProviderRepository
.findOne(providerIdLong);
LOG.debug("fuelProvider:" + fuelProvider.getName());
List<AirportFuel> airportFuels = processors
.get(providerIdLong).process(serverFile, fuelProvider);
if(airportFuels==null){
return new AsyncResult<>(false);
}
airportFuelRepository.deleteByFpId(providerIdLong);
airportFuelRepository.save(airportFuels);
fuelProvider.setUpdated(new Date());
fuelProviderRepository.save(fuelProvider);
return new AsyncResult<>(true);
}
For example this is read method:
List<AirportFuel> airportFuels = airportFuelRepository.findByIata(airport.getIata());
and AirportFuelRepository:
#Repository
#Transactional(readOnly = true)
public interface AirportFuelRepository extends CrudRepository<AirportFuel,
Long> {
#Transactional
#Modifying
#Query("delete from AirportFuel af where af.fpId = :#{#fpId}")
void deleteByFpId(#Param("fpId") Long fpId);
#Transactional(readOnly = true)
List<AirportFuel> findByIata(String iata);
List<AirportFuel> findByIataAndFpId(String iata, Long fpId);
}
Application is based on Spring boot, Hibernate, Spring data JPA and MS SQL.
If import method running then other method which also use airportFuelRepository waiting for end of this method and i do not know why. I suppose the reading method will work with the data before the import and will not waiting to end import method.
Thank you for advice.
If I understand correctly you want to have both methods be able to access the same repository asynchronously.
In order to do this annotate your method with the #Async annotation.
A few links to get you started:
baeldung.com
Spring Quickstart
Spring Javadoc
Dzone

Issue with Static resources when extending Spring Boot WebMvcConfigurationSupport

I extended WebMvcConfigurationSupport to implement an api versioning scheme - i.e.
#Configuration
public class ApiVersionConfiguration extends WebMvcConfigurationSupport {
#Override
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
return new ApiVersionRequestMappingHandlerMapping(readDateToVersionMap());
}}
This uses a custom handler mapping to version the api and works quite nicely.
However it also seems to disable the #EnableAutoConfiguration bean so that now static resources aren't served (as mentioned in this question Is it possible to extend WebMvcConfigurationSupport and use WebMvcAutoConfiguration?).
Ok, I thought, let's just add a resource handler to the class above - i.e.
#Configuration
public class ApiVersionConfiguration extends WebMvcConfigurationSupport {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("classpath:/public/").addResourceLocations("/");
}
#Override
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
return new ApiVersionRequestMappingHandlerMapping(readDateToVersionMap());
}}
However.. this isn't working..? I get this error when I browse to /index.html:
No mapping found for HTTP request with URI [/index.html] in DispatcherServlet with name 'dispatcherServlet'
..If I disable this class then these resources are served just fine by #EnableAutoConfiguration magic.
I've been playing with various options to serve static content having extended the WebMvcConfigurationSupport and thus far no success.
Any ideas?
I was facing the same problem and came up with a solution that just works for me. If you just want to get the resources working without worrying of repetition you can do:
#Configuration
public class StaticResourcesConfig extends WebMvcAutoConfigurationAdapter {
}
and then
#Configuration
#EnableWebMvc
#Import(StaticResourcesConfig.class)
public class WebConfig extends WebMvcConfigurationSupport {
...
}
This successfully uses the Spring Boot defaults for serving static resources, as long as you don't map /** in your controllers.

#EnableAspectJAutoProxy not work with proxyTargetClass=false

I am learning about Spring AOP at first time.
I am reading about in this sites:
Site2 and
Site1
Following this I have made the next classes
Main class:
public class App {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(AppConfig.class);
context.refresh();
MessagePrinter printer = context.getBean(MessagePrinter.class);
System.out.println(printer.getMessage());
}
}
App config class:
#Configuration
#ComponentScan("com.pjcom.springaop")
#EnableAspectJAutoProxy(proxyTargetClass=true)
public class AppConfig {
#PostConstruct
public void doAlert() {
System.out.println("Application done.");
}
}
Aspect class:
#Component
#Aspect
public class AspectMonitor {
#Before("execution(* com.pjcom.springaop.message.impl.MessagePrinter.getMessage(..))")
public void beforeMessagePointCut(JoinPoint joinPoint) {
System.out.println("Monitorizando Mensaje.");
}
}
And others...
Just like that app work nice, but if I put proxyTargetClass to false. Then I get the error below.
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.pjcom.springaop.message.impl.MessagePrinter] is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:318)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:985)
at com.pjcom.springaop.App.main(App.java:18)
Why?
#EnableAspectJAutoProxy(proxyTargetClass=false)
Indicates that JDK dynamic proxy will be created to support aspect execution on the object. And therefore as this type of proxy requires a class to implement an interface your MessagePrinter must implement some interface which declares method getMessage.
#EnableAspectJAutoProxy(proxyTargetClass=true)
On the opposite instruct to use CGLIB proxy which is able to create proxy for a class without an interface.
1> Message Printer has to be defined as a component i.e :
`
package com.pjcom.springaop.message.impl;
#Component
public class MessagePrinter{
public void getMessage(){
System.out.println("getMessage() called");
}
}`
in the same package as configuration java file if no #ComponentScan is not defined for some other packages.
2> If same type of bean class has many other dependencies then to resolve dependencies in spring Config use #Qualifier annotation.

Resources