how to redirect spring security to custom login page in spring-boot? - spring

I am creating a spring boot application. I want to add custom login page into it. How do i do that?
I am attaching my structure just for reference.
my file structure

By applying spring security If application.properties does't work you can apply the
#Bean
public InternalResourceViewResolver jspViewResolver() {
InternalResourceViewResolver resolver= new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
method it will work.

Related

Why application.properties is not executing properties after applying spring security in spring boot application?

I have created an spring-boot application. It was working fine all the css and js were mapping perfectly with my jsp pages and application was able to map to my jsp pages as well. By appication.properties file in resources folder.
spring.mvc.view.prefix = /WEB-INF/views/
spring.mvc.view.suffix = .jsp
spring.mvc.static-path-pattern=/resources/**
server.port=8181
But since I have enabled Spring security I am not able to do that I needed to initialise #bean class
#Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
It is weird. Can anybody help me with it?
Thank you in advance,
Priyal shah.
Ensure that your class is extending WebMvcConfigurerAdapter class like below sample.
#Configuration
#ComponentScan(basePackages="com.package")
#EnableWebMvc
public class MvcConfigs extends WebMvcConfigurerAdapter{
#Bean
public ViewResolver getViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}

Spring Boot urls with .html ending

I have very simple hello world created with spring boot (just starter web, no thymeleaf etc.).
I want to handle /hello url and it should produces view from /static/view.html
I have /static/view.html and simple method in my controller:
#RequestMapping("/hello")
public String hello2() {
return "hello.html";
}
The problem is that it causes error:
Circular view path [hello.html]: would dispatch back to the current
handler URL [/hello.html] again
I figured out that it does not matter if I visit /hello or /hello.html, Spring treats them the same.
How can I return simple, static html with the same name as the url path and which object in spring mvc/boot causes mapping url like /example.html to just /example?
You have to perform next steps:
Introduce MVC configuration:
#Configuration
#EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
#Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/");
return resolver;
}
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
Put your hello.html into /src/main/webapp/WEB-INF folder
Make sure you have compile dependencies like this:
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.apache.tomcat.embed:tomcat-embed-jasper")
I posted gradle code, if you have maven use similar xml.
NOTE Step 3 is the most important step because if you're using spring boot it applies auto-configurations depending on what you have in classpath. For instance if you add thymeleaf dependency
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
that will most likelly break the code because thymeleaf will introduce its own auto-configured view resolver.
Delete the method in your controller. A #RequestMapping is not needed for static content.

Spring boot: InternalResourceViewResolver not working

I spent several hours trying to use InternalResourceViewResolver in order to append prefix and suffix to html views.
My views located under static/pages/ and by Spring docs, folder static is considered to be one of defaults for static content. So, I could access profile page by pages/profile.html. But what I really want to have is profile instead of pages/profile.html.
I've tried several answers, but that does not work, like:
#Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("pages/");
resolver.setSuffix(".html");
return resolver;
}
and adding
#Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
Still does not work properly. By adding any suffixes or prefixes I could not found page on any path. I am starting to get 404 on pages/profile.html, but it also does not appear on other urls.
Just need add your own custom configuration like this
#Configuration
public class WebMvcConfig {
#Bean
public InternalResourceViewResolver defaultViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/jsp");
resolver.setSuffix(".jsp");
return resolver;
}
}
Then you can inspect all your beans via "http://localhost:8080/beans"
And you can verfity is it using the custom configured bean:
{
"bean": "defaultViewResolver",
"scope": "singleton",
"type": "org.springframework.web.servlet.view.InternalResourceViewResolver",
"resource": "class path resource [io/cloudhuang/web/WebMvcConfig.class]",
"dependencies": [ ]
}
But the eaiest way should be config it in the application.properties
spring.mvc.view.prefix=
spring.mvc.view.suffix=
For application.yaml
spring:
mvc:
view:
prefix: templates/
suffix: .jsp
Using Spring Boot you actually don't need to declare your own InternalResourceViewResolver. Boot declares it for you, and you can just add a couple of properties to your application.properties file. E.g. in your case these would be:
spring.mvc.view.prefix=/jsp
spring.mvc.view.suffix=.jsp

How to integrate Spring Boot and Resteay

Although there are a lot of code example on the internet of integrating Spring Boot/Spring and Resteasy, but most of them are out of data or even don't work.
I am look at the latest Resteasy document, try to create a instance of SpringBeanProcessorServletAware in my config bean.
#Bean
public ServletListenerRegistrationBean<ResteasyBootstrap> resteasyBootstrapRegistratio() {
ServletListenerRegistrationBean<ResteasyBootstrap> registration = new ServletListenerRegistrationBean<>();
registration.setListener(new ResteasyBootstrap());
return registration;
}
#Bean
public ServletRegistrationBean resteasyServletRegistratio() {
ServletRegistrationBean registration = new ServletRegistrationBean();
registration.setServlet(new HttpServletDispatcher());
registration.addUrlMappings("/*");
return registration;
}
#Bean
public SpringBeanProcessorServletAware springBeanProcessorServletAware() {
SpringBeanProcessorServletAware springBeanProcessor = new SpringBeanProcessorServletAware();
return springBeanProcessor;
}
But it will throw Nullpoint exception. It seems like the servletContext is required to make SpringBeanProcessorServletAware work.
Then I try to inject servletContext. But the bean SpringBeanProcessorServletAware is being created before ServletContextInitializer finished.
How to make some bean created after ServletContextInitializer is finished?
Am I wrong in do the integration between Spring Boot and Resteasy.

spring mvc request not available error

#Bean
public ViewResolver viewResolver()
{
InternalResourceViewResolver resolver=new InternalResourceViewResolver();
resolver.setPrefix("WEB-INF/views");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
#RequestMapping(value="/events/{id}")
public String showEvent(#PathVariable("id") int id)
{
return "/events/show";
}
o/p: when i use url: http://localhost:8080/tracker/events/1
HTTP Status 404 - /tracker/events/WEB-INF/views/events/show.jsp
type Status report
message /tracker/events/WEB-INF/views/events/show.jsp
description The requested resource is not available.
Apache Tomcat/7.0.56
i am using spring 4.1 with java configuration and annotations. is there any thing wrong with configuration?

Resources