Can I use two interceptors on same spring boot application? - java

I have a Spring Boot application that contains two different dashboards, and so each user can access only one. Actually I have one interceptor that secure one dashboard, but the second dashboard needs another interceptor. This is possible?
This is my configuration class:
#Configuration
public class SecurityConfiguration implements WebMvcConfigurer{
#Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new AutorizadorInterceptorEscola());
}
}
I need to add another interceptor to secure the second dashboard.

Create Configuration Class extends with WebMvcConfigurerAdapter
#Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new GuestInterceptor());
registry.addInterceptor(new AdminInterceptor());
}

Related

Resources are called from /appName but from root

I'm usign spring boot.
When I deploy my app under a AWS balancer, the app is calling some resources from myDir/myAppName. But the style resources are called from root, like myDir/.
E.g. myDir/myAppName/index.html is accesible for loadBalancer. But myDir/myStilesheet.css isn't accesible.
How can I set that resources (css, etc) under myDir/myAppName?
Thanks in advance!
You can implement the WebMvcConfigurer interface and override the void addResourceHandlers(final ResourceHandlerRegistry registry) method.
#Configuration
public class MvcConfig implements WebMvcConfigurer {
#Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("myDir/myAppName/myStilesheet.css")
.addResourceLocations("classpath:/resources/static/css/myStilesheet.css");
}}

Is there any way to inject Interceptor from external library in Spring?

I'm developing a jar library and trying to inject an interceptor from external jar library to Application.
For example:
External Lib
MyExternalInterceptor.java
public class MyExternalInterceptor implements HandlerInterceptor {
#Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
// Do something
}
}
I tried to using AOP in external libs but it's not working.
InterceptorAspect.java
#Around("execution(* org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.addInterceptors(..))")
public Object aspect(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
// Tried to inject MyExternalInterceptor here
Object result = proceedingJoinPoint.proceed();
return result;
}
In Application using that lib:
Application
MyConfiguration.java
#Configuration
public MyConfiguration extends WebMvcConfigurationSupport {
#Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new SpringTestInterceptor()); // client's own interceptor
/* Add MyExternalInterceptor not explicitly but implicitly using AOP or other things */
}
}
Is there any way to inject an interceptor from external lib to App?
I know the question is very obscure (sorry for that), but could you give me any advice or hint to make it work?
Thank you for anyone who read my question :)
(I updated few more details for clarification)
Summary
Use WebMvcConfigurer in both Client and library side instead of WebMvcConfigurationSupport
AoP is not needed
I use WebMvcConfigurer instead of WebMvcConfigurationSupport and change some codes like below:
External Lib
MyExternalInterceptor.java
Same as before
InterfaceAspect.java
Don't needed it anymore
MyExternalLibConfiguration.java
#Configuration
public class MyExternalLibConfiguration implements WebMvcConfigurer {
#Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyExternalInterceptor());
}
}
Application (client)
MyConfiguration.java
#Configuration
public MyConfiguration implements WebMvcConfigurer {
#Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new SpringTestInterceptor()); // client's own interceptor
/* No need to add MyExternalInterceptor in here */
}
}
That's all! Everything is working well as M. Deinum said in comment.
Thank you again Deinum!

Mapping to app root "/" is failing, but other than that is working

I'm using Spring Boot 2.5.2. I configured all things (not security, it is auto configured and need to convert all configuration from XML to Java. Authentication is working via LDAP) and working correctly except one thing i.e. mapping to / -> redirect:/main.
Note: This is a migration from Spring to Spring Boot with .jsp as view.
Below is the mapping in XML:
<mvc:view-controller path="/" view-name="redirect:/main" />
Above is working correctly in Spring but when converted to Spring Boot using Java code as below then this specific case is not working:
#Configuration
public class WebMvcConfig implements WebMvcConfigurer {
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("redirect:/main");
}
}
Note: If I set addViewController something else like /documentation then it is redirecting successfully but not with /.
you can use addRedirectViewController
#Configuration
public class WebConfiguration implements WebMvcConfigurer {
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/", "redirect:/main");
}
}
After trying many things, finally got the solution. I need to setOrder() to highest precedence to work. But I still don't know why I need to do this for this specific case. Below is the code:
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("redirect:/main");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
When using addRedirectViewController() one have to set url name properly:
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/", "/yourURL");
}
but NOT "redirect:/yourURL"

Internal Server Error When Adding WebMvcConfigurer To Spring Boot Rest Application

I have a Spring Boot app and I wanted to allow other origins to make requests because I got the 'cors' error. So I searched and I found this answer: Annotation CrossOrigin not working in Spring boot witch helped for the endpoints that have no body. On the other hand, those who have a body I get Internal Server Error.
Below is the configuration:
#Configuration
#EnableWebMvc
public class CorsConfiguration implements WebMvcConfigurer {
#Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:63343", "http://localhost:3000", "https://smart-booking-ba548.web.app")
.allowedMethods("GET", "POST", "DELETE", "PUT");
}
}
The controllers have the #RestController annotation and the methods #Get/Post|Mapping. They return a ResponseEntity<Object>.
I solved it by putting the following method in main class.
#Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
#Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*");
}
};
}

Cannot Use InterceptorRegistry While the Application Extend Neo4jConfiguration

I am using spring boot application mvc with graphDb (Neo4j) as my database.
And I have problem when I have to do internationalization for my app.
I have this code on my application.java
public class Application extends Neo4jConfiguration {
#Autowired
GraphDatabase graphDatabase;
#Bean
GraphDatabaseService graphDatabaseService() {
return new GraphDatabaseFactory().newEmbeddedDatabase("my-graphdb");
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
When I tried to implement internationalization, the tutor says that I need to implement:
#Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
This code above need the class is extended by WebMvcConfigurerAdapter.
This is the problem, I didn't use WebMvcConfigurerAdapter so I cannot add the method above.
Do I have another option to make my internationalization work well with Neo4jConfiguration?
I just found out how to make it work.
I just have to add new file that extend WebMvcConfigurerAdapter and put the addInterceptors module into that new file.
Which tutorial are you referring to?
you can also create your own Neo4jConfiguration subclass and #Import it onto your boot application context

Categories

Resources