Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

I have a Springframework project that had a property value that contained the host of the server that I was going to be deployed on. There was more to it, because there were profiles, etc, but that is the gist of it. There is a value in a property file that I need.

The properties file was declared as:

<util:properties id="commonProperties" location="classpath:common-default.properties"/>

The contents of the properties file contained:

hostUrl=localhost:8080

To use the value in the jsp, pull in the Spring tags by:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

Tie it all together by

<spring:eval var="loginUrl" expression="@commonProperties.hostUrl"/>
<a href="http://${loginUrl}/hub/login.jsp">Return to login</a>

January 27th, 2014

Posted In: JSP, SpEl, Spring

Leave a Comment

The servlet 3.0 spec defines a way to share webapp JSP resources via a dependent jar (See Servlet 3.0 spec, section 10.5)

If you have a Maven dependency that is a jar, you can package resources and have them available to webapps that use them as a dependency.

To share, /css/test.css, you need to place it in:
/mavenModule/src/main/resources/META-INF/resources/css/test.css

Any webapp that depends on that jar can access /css/test.css as if it was in the root of it’s own webapp.

<link rel="stylesheet" type="text/css" href="<c:url value="/css/test.css" />">

Caveat, IntelliJ does not resolve /css/test.css as being within the current webapp. There was a ticket that was filed in 2011, but it doesn’t appear that it will ever be fixed.

Note, that if you are trying to use the included file as a tag file, it will need to have /WEB-INF/tags at the front of the included path.

January 24th, 2014

Posted In: IntelliJ, Maven, Servlet Spec

Leave a Comment

There will be sometimes in your code that you need access to the Springframework security principal object. Here is how you get it. You will need to create a Spring configuration class and annotate it with @EnableWebMvcSecurity

package com.cdi.igs.hub.spring;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;

/**
 * Spring Security configuration.
 * @author norris.shelton
 */
@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    // nothing needed here
}

The documentation then says to add a method parameter to a Spring MVC controller method with @AuthenticationPrincipal and you are done, like the following:

    @RequestMapping(value = "/{personId}", method = RequestMethod.GET)
    public ModelAndView getPersonAccount(@PathVariable int personId, @AuthenticationPrincipal User user) {
        ModelAndView modelAndView =  new ModelAndView("dashboard/account");
        modelAndView.addObject("person", personRepository.findOne(personId));
        return modelAndView;
    }

What they don’t tell you is that you need to configure a AuthenticationPrincipalArgumentResolver. Nor they tell you how to do it. This is the missing piece.

    <mvc:annotation-driven>
        <mvc:argument-resolvers>
            <bean class="org.springframework.security.web.bind.support.AuthenticationPrincipalArgumentResolver"/>
        </mvc:argument-resolvers>
    </mvc:annotation-driven>

January 14th, 2014

Posted In: Spring, Spring MVC, Spring Security

Tags: , , , , ,

17 Comments

LinkedIn Auto Publish Powered By : XYZScripts.com