Spring4 – Accessing static resources via java configuration

Project structure

projectstructure_201412231730

Java configuration

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = “com.ggr.jsr”)
public class AppConfiguration extends WebMvcConfigurerAdapter {

@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix(“/WEB-INF/views/”);
viewResolver.setSuffix(“.jsp”);
return viewResolver;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(“/resources/**”).addResourceLocations(
“/resources/”);
}

}

JSP reference

<script src=”<c:url value=’/resources/js/bootstrap.min.js’/>”></script>

ExceptionTranslationFilter – Access is denied (user is anonymous)

Problem

Log error message


DEBUG: org.springframework.security.web.access.ExceptionTranslationFilter - Access is denied (user is anonymous); redirecting to authentication entry point
org.springframework.security.access.AccessDeniedException: Access is denied
at org.springframework.security.access.vote.AffirmativeBased.decide(AffirmativeBased.java:83)
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:206)
at ......

Solution

This issue occurs due to following reason

  • Incorrect order of element in spring security config file.
  • ‘Permit all’ is not sufficient for anonymous users. Due to which we need to add role ‘ANONYMOUS’ for desired pages.

protected void configure(HttpSecurity httpSecurity) throws Exception
{
httpSecurity.requiresChannel().anyRequest().requiresSecure()
// Configures url based authorization
.and().authorizeRequests()
// Anyone can access the urls
.antMatchers("/auth/**", "/login", "/signup", "/forgotPassword").permitAll()
// The rest of the our application is protected.
.anyRequest().hasAnyRole("ANONYMOUS, USER")

// Configures form login
.and().formLogin().loginPage("/login").failureUrl("/login?error=bad_credentials")
// Configures the logout function
..........
.and().rememberMe().tokenValiditySeconds(1209600).and().exceptionHandling().accessDeniedHandler(accessDeniedHandler());
}

Parsererror: SyntaxError: Unexpected token Y

Problem

ERRORS: parsererror
singleUpload:67 ERROR THROWN: SyntaxError: Unexpected token Y
singleUpload:68 ERRORS desc: [object Object]

Solution

Changed dataype of ajax.

 

$.ajax({
url : '/FileUpload/singleSave',
type : 'POST',
data : data,
cache : false,
dataType : 'json', // The type of data that you're expecting back from the server.
processData : false, // Don't process the files
contentType : false, // Set content type to false as jQuery will tell the server its a query string request
xhrFields : {
onprogress : function(e)
{

Modified to

dataType : ‘text’, // The type of data that you’re expecting back from the server.

 

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile

Problem


mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building XXX 1.0.0-BUILD-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[WARNING] The POM for org.springframework.social:spring-social-core:jar:1.1.0.BUILD-SNAPSHOT is missing, no dependency information available
[WARNING] The POM for org.springframework.social:spring-social-config:jar:1.1.0.BUILD-SNAPSHOT is missing, no dependency information available
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mb ---
[WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 3 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mb ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding MacRoman, i.e. build is platform dependent!
[INFO] Compiling 43 source files to /......
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.109s
[INFO] Finished at: Sat Dec 06 09:00:15 PST 2014
[INFO] Final Memory: 7M/81M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project mb: Fatal error compiling: invalid target release: 1.7 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Solution

invalid target release: 1.7 indicates that the project is pointed to java 1.7 but maven is not.
Make sure maven is also pointed to correct jdk.

In my case, Java was pointing to jdk 1.7 but maven wasn’t.

java -version
java version “1.7.0_45”
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)

$ mvn –version
Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-17 08:22:22-0700)
Maven home: /……/apache-maven-3.1.1
Java version: 1.6.0_65, vendor: Apple Inc.
Java home: /…./1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: “mac os x”, version: “10.9.5”, arch: “x86_64”, family: “mac”

Update maven configuration to point to java 1.7 to resolve this issue.