Spring boot & Spring Security : Forward the invalid urls - angularjs

I work with spring boot, thymeleaf, spring security and angularjs
I want all url invalid to return to the home page.
The login page is managed by thymeleaf and the rest by angularjs
So i used a #Controller to forward all invalid url to the localhost:8080/:
#Controller
public class AccueilController {
// Match everything without a suffix (so not a static resource)
#RequestMapping(value = "/{path:[^\\.]*}")
public String redirect() {
// Forward to home page so that route is preserved.
return "forward:/";
}
}
when connecting if I type localhost:8080/XXXXthat forward me to the racine after login but if I type localhost:8080/XXXX/XXXX that give me a 404 error
after connection I don't have that problem because Angular forward all url correctly.

That's not the default behavior of the web applications. I sugest you to customize the not found page (Spring Boot and custom 404 error page) and then from there create a link to your home;

Related

Blazor authentication with Azure Active Directory and custom base URI

I have a blazor webassembly application that implements authentication using Azure Active Directory. Everything works fine.
Now I need to change the base URI of the application from / to /app. I did that following the steps here.
Doing the above broke the authentication flow. Now the application is stuck in this url https://localhost:7105/app/authentication/login?returnUrl=https%3A%2F%2Flocalhost%3A7105%2Fapp.
Looking in the console I found the following stack :
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Invalid return url. The return url needs to have the same origin as the current page.
System.InvalidOperationException: Invalid return url. The return url needs to have the same origin as the current page.
at Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticatorViewCore`1[[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationState, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=6.0.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].GetReturnUrl(RemoteAuthenticationState state, String defaultReturnUrl)
at Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticatorViewCore`1.<OnParametersSetAsync>d__82[[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationState, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=6.0.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].MoveNext()
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
It says the return url is invalid. I can't figure out where this return url is set and what to change it for.
Thanks to #TinyWang for pointing to the github issue. The workaround was to append a "/" at the end of the return url:
var uri = Navigation.Uri;
if (uri.EndsWith("/app")) uri += "/";
Navigation.NavigateTo($"authentication/login?returnUrl={Uri.EscapeDataString((uri))}");
To add my two cents, since in this case the return url is the landing page of the app, this works fine too:
Navigation.NavigateTo($"authentication/login?returnUrl={Uri.EscapeDataString(Navigation.BaseUri)}");

firebase 404 page configuration in angularjs

I try to add a 404 page into my angularjs project which is hosted on firebase servers. I created a custom 404 page and added following code:
function routeConfig($stateProvider, $urlRouterProvider, $locationProvider)
{
$urlRouterProvider.otherwise('/pages/errors/error-404');
}
Now it works but I am missing something because when I check in the browser's network panel the status code is "200" but it should be "404".
What should I do for that?
$urlRouterProvider.otherwise just guides AngularJS that which URL it should navigate to when no URL matches the provider's rules.
This doesn't signify that a resource is not found on the server (which essentially returns 404). To do that, you need to do a lot more including serving this 404 page from the server with 404 as the status code.
Here is a resource to point you in the right direction:
AngularJS UI router handling 404s

resolving views in angularjs spring boot application with spring security

I am trying to build an application with AngularJS 1.x, Spring Boot REST with Spring Security. Though I have intermediate knowledge on Spring REST itself, I am quite new to AngularJS and Spring Security.
I have given the github link of the application I have developed so far. Its in skeletal form still, and nothing is working:
https://github.com/imrnk/touchinghand
I will list down the problems and confusion I am having below:
1) I am using ui-router states to navigate from one state to another. So far I have identified two states: the "login" page and a link from there to "registration" page. Once the user will logged in, she will land to a dashboard. But this dashboard is yet to be created.
Now this login.html could be said as the entry point to the application. And when I type localhost:8080/ it should redirect to localhost:8080/login. Now I can see the page is redirecting correctly to the login.html but the templates I am using (login.template.html or register.template.html) inside login.html is not loading... However, when I am running through node js using browsersync, I see the page is loading with all the contents in it.
2) I tried disabling the spring security and then I see the login page is loaded correctly. So I guessed it could be a spring security issue, but what exactly is the issue I couldn't figure out.
My HttpSecurity configuration inside the implementation of WebSecurityConfigurerAdapter looks like this:
#Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/login", "/register", "/**/*.css",
"/**/*.js", "/**/**/*.css", "/**/**/*.js",
"/**/**/**/*.css", "/**/**/**/*.js",
"/**/**/**/**/*.css", "/**/**/**/**/*.js", "/**/home.html", "**/login.html",
"**/**/login.template.html", "**/**/registration.template.html")
.permitAll().
regexMatchers("/registration*")
.permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").permitAll()
.and()
.logout().permitAll();
}
And my MvcConfig looks like this:
#Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
registry.addResourceHandler("classpath:/resources/static/css/**")
.addResourceLocations("classpath:/resources/static/css/");
registry.addResourceHandler("classpath:/resources/js/**").addResourceLocations("classpath:/resources/js/");
registry.addResourceHandler("classpath:/resources/static/**")
.addResourceLocations("classpath:/resources/static/");
registry.addResourceHandler("classpath:/resources/static/templates/**")
.addResourceLocations("classpath:/resources/static/templates/");
}
#Override
public void addViewControllers(ViewControllerRegistry registry) {
super.addViewControllers(registry);
registry.addViewController("/").setViewName("forward:/login");
//registry.addViewController("/login").setViewName("forward:/login");
registry.addViewController("login.html");
//registry.addViewController("register").setViewName("registration");
// registry.addViewController("/registration.html");
//registry.addViewController("/dashboard").setViewName("dashboard");
//registry.addViewController("/dashboard.html");
}
#Override
public void configureViewResolvers(ViewResolverRegistry registry) {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("static/");
resolver.setSuffix(".html");
registry.viewResolver(resolver);
}
}
My LoginController class which anotated as #Controller has this:
#RequestMapping(value={"/"}, method = RequestMethod.GET)
public String showLogin(Model model){
model.addAttribute("user", new UserDTO());
return "/login";
}
3) I am quite confused between what should be the name of the resolved views and how that could map with the angular templates html.
I suspect, probably I am following a pure REST approach, and hanging in the middle with some aspect of Spring MVC and Spring REST. Introducing Spring Security also increased the problem for now.
I am also attaching the firefox developer network screenshot if that could help
enter image description here
Kindly help me out. Thanks.

Spring MVC and AngularJS routing conflict

Hi Guys.
I have problem with routing between Spring MVC and AngularJS.
So, when I got homepage : http://localhost:8080/ captures that Angular and its ok but when i want go to another page e.g http://localhost:8080/dashboard captures that Spring MVC and Tomcat show error 404 Not Found.
To get to dashboard in need use url /#/dashbaord
Although i got below code in my file with routing configuration.
It is possible to angular was first taken into consideration and next Spring MVC?
$locationProvider.html5Mode({
enabled : true,
requireBase : false
});
Regards
You need to route all Tomcat URLs to index.html
For API calls to Spring Boot http://localhost:8080/springbootapi/wotever
And in Tomcat say DO NOT POINT ANY URL WITH /springbootapi/ in it to index.html
EVERYTHING ELSE TO index.html
Pointing everything else to index.html gives Angular full control over everything except API calls
The above outline the generic approach to do this.
Maybe better to follow this tutorial ...
https://spring.io/guides/gs/consuming-rest-angularjs/

Error 404 - connect to Gmail API from MVC Application

I have problem. I try to connect with Gmail API from my MVC application. On my local machine all works fine but when I deploy application to IIS I get 404 error. It is caused by redirect application to "http://localhost/AuthCallback/IndexAsync" address when the correct is "http://localhost/myApplicationName/AuthCallback/IndexAsync".
Can I change default redirect url?
I get error after user authorization.
I'm using Google.Apis Version 1.9.1.12395 and GoogleApis.Gmail.v1 version 1.9.0.31.
Ok, I resolve problem. I had to override parameter in class AppFlowMetadata
public override string AuthCallback
{
get
{
return #"/ApplicationName/AuthCallback/IndexAsync";
}
}

Resources