I know that this combination of technologies and issues have been asked a lot before but I am unable to proceed even after reading most of the related questions.
My set up involves an angular app served by a spring boot end point. All is well on the dev front with all requests being served up correctly. So I installed a local tomcat (tried 10 first and got nowhere with it, so went down to 9). The steps I did are
- For angular dist creation
ng build --base-href=/audittool/
- Copied dist contents into the my src/main/resources folder of spring boot project
- Changed pom to package as war and added provided scope for tomcat, extended SpringBootServletInitializer and performed a
mvn clean install
- Fired up manager-gui on my tomcat, deployed the created war (this has the same name as the base href used in the ng build step, audittoo)
- War was deployed successfully
Now when I access http://localhost:8082/audittool I am getting
This application has no explicit mapping for /error, so you are seeing this as a fallback.
There was an unexpected error (type=Unauthorized, status=401).
I have done most of the steps manually (copying dist folder) as I just wanted to test my app out on a dedicated tomcat instance.
I have read enough and more on this topic and I still do not know what I am missing. I know that the 401 could be CORS, but the base href is supposed to solve that as long as the war deploy name matches, which it does. So why am I not even being served index.html, no static resource is being downloaded.
I have nothing special in my POM other than the usual stuff (and one WSDL that I use for auth)
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.abc</groupId>
<artifactId>audittool</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>audittool</name>
<description>spring boot project for audit tool</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>audittool</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.13.2</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<generatePackage>com......soaplloginservice.gen</generatePackage>
<generateDirectory>${project.basedir}/src/main/java</generateDirectory>
<schemaDirectory>${project.basedir}/src/main/resources</schemaDirectory>
<schemaIncludes>
<include>*.wsdl</include>
</schemaIncludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
When I run a postman request it goes through fine using the http://localhost:8082/audittool/ So it is only when I try to get to the angular front end that I am getting into trouble.
Any advice is most appreciated.
My SecurityConfig class is as below:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private WebServiceAuthenticationProvider authProvider;
@Autowired
private JWTRequestFilter jwtFilter;
@Autowired
private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authProvider);
}
@Bean
public PasswordEncoder passwordEncoder(){
return NoOpPasswordEncoder.getInstance();
}
@Bean(name = BeanIds.AUTHENTICATION_MANAGER)
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().cors().and().
authorizeRequests().antMatchers("/authenticate")
.permitAll().antMatchers(HttpMethod.OPTIONS, "/**")
.permitAll().anyRequest().authenticated()
.and().exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint)
.and().sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class);
}
}
My LoginController which is the first end point hit for authentication has the @CrossOrigin annotation. At least up until this I should be able to get through via angular right? I straightaway get the unauthorized page :(
Can someone please help me out here as to what I am missing? This is driving me nuts.
Thanks!