
The following is my schedule for the Devoxx conference. In red the presentations I will attend. If you would like to suggest me other presentations you would like to hear about from me, send me a line and I will try to attend them.
NetBeans: Executing 'mvn -Dnetbeans.execution=true compiler:compile'
NetBeans: JAVA_HOME =/usr/lib/jvm/java-6-sun
Scanning for projects...
Searching repository for plugin with prefix: 'compiler'.
------------------------------------------------------------------------
Building minimark Web Application
task-segment: [compiler:compile]
------------------------------------------------------------------------
[compiler:compile]
Compiling 2 source files to /home/lucio/MyWorks/minimark/minimark/target/classes
------------------------------------------------------------------------
BUILD SUCCESSFUL
------------------------------------------------------------------------
Total time: 3 seconds
Finished at: Sun Aug 09 08:14:56 CEST 2009
Final Memory: 18M/144M
------------------------------------------------------------------------
JavaRebel: Reloading class 'com.benfante.minimark.controllers.HomeController'.
JavaRebel-Spring: Reconfiguring bean 'homeController' [com.benfante.minimark.controllers.HomeController]
JavaRebel: Reloading class 'com.benfante.minimark.blo.UserProfileBo'.
JavaRebel-Spring: Reconfiguring bean 'userProfileBo' [com.benfante.minimark.blo.UserProfileBo]
org.zeroturnaround
javarebel-maven-plugin
generate-rebel-xml
process-resources
generate
CATALINA_OPTS="-XX:PermSize=32m -XX:MaxPermSize=200m -Xmx256m -Dfile.encoding=UTF-8 -noverify -javaagent:/home/lucio/local/javarebel-2.0/javarebel.jar -Drebel.spring_plugin=true"
The logged user must select its current role among the roles for which he's authorized.
public class LoggedUserWithSelectableRole extends User {
private GrantedAuthority currentAuthority;
public LoggedUserWithSelectableRole(String username, String password,
boolean enabled, GrantedAuthority[] authorities) throws IllegalArgumentException {
super(username, password, enabled, authorities);
}
public void setCurrentAuthority(GrantedAuthority currentAuthority) {
this.currentAuthority = currentAuthority;
}
@Override
public GrantedAuthority[] getAuthorities() {
if (Arrays.asList(super.getAuthorities()).contains(currentAuthority)) {
return new GrantedAuthority[] {currentAuthority};
} else {
return new GrantedAuthority[0];
}
}
public GrantedAuthority[] getAllAuthorities() {
return super.getAuthorities();
}
}
@RequestMapping
public String selectRole(@RequestParam(value = "role") int role) {
LoggedUserWithSelectableRole user =
(LoggedUserWithSelectableRole) SecurityContextHolder.getContext().
getAuthentication().getPrincipal();
user.setCurrentAuthority(user.getAllAuthorities()[role]);
return "redirect:/";
}
/* WARNING: The method setAuthorities doesn't exist */
SecurityContextHolder.getContext().getAuthentication().
setAuthorities(user.getAuthorities());
setAuthorities
doesn't exist. Worst, in the AbtractAuthenticationToken
class, the base class of most of the token implementations, the authorities
attribute is private, so you can't easily implement by yourself an alternative token implementation extending the original token class.
public class UpdatableCasAuthenticationToken extends CasAuthenticationToken {
private final int keyHash;
public UpdatableCasAuthenticationToken(CasAuthenticationToken token, GrantedAuthority[] authorities) {
super("BOH", token.getPrincipal(), token.getCredentials(), authorities, token.getUserDetails(), token.getAssertion());
this.keyHash = token.getKeyHash();
}
@Override
public int getKeyHash() {
return this.keyHash;
}
}
@RequestMapping
public String selectRole(@RequestParam(value = "role") int role) {
LoggedUserWithSelectableRole user =
(LoggedUserWithSelectableRole) SecurityContextHolder.getContext().
getAuthentication().getPrincipal();
user.setCurrentAuthority(user.getAllAuthorities()[role]);
SecurityContextHolder.getContext().setAuthentication(
new UpdatableCasAuthenticationToken(
(CasAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(),
user.getAuthorities()));
return "redirect:/";
}
CasAuthenticationToken
should provide a way for updating authorities, and maybe the authorites
attribute of AbstractAuthenticationToken
should be declared as protected
.
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.parancoe.plugin.tiles.CheapTilesView"/>
</bean>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="template.main" template="/WEB-INF/tiles/templates/main.jsp">
<put-attribute name="header" value="/WEB-INF/jsp/header.jsp"/>
<put-attribute name="menu" value="/WEB-INF/jsp/menu.jsp"/>
<put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp"/>
</definition>
</tiles-definitions>
admin/conf
and a definition with that name doesn't already exist in your configuration file, the CheapTilesView
class will generate (at runtime) this for you:
<definition name="admin/conf" extends="template.main">
<put-attribute name="main" value="/WEB-INF/jsp/admin/conf.jsp"/>
</definition>
CheapTilesView
can be customized passing some attributes to the view resolver. The following are the default values:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.parancoe.plugin.tiles.CheapTilesView"/>
<property name="attributesMap">
<map>
<entry key="org.parancoe.plugin.tiles.CheapTilesView.DEFAULT_TEMPLATE" value="template.main"/>
<entry key="org.parancoe.plugin.tiles.CheapTilesView.DEFAULT_ATTRIBUTES" value="main"/>
<entry key="org.parancoe.plugin.tiles.CheapTilesView.DEFAULT_PREFIX" value="/WEB-INF/jsp/"/>
<entry key="org.parancoe.plugin.tiles.CheapTilesView.DEFAULT_SUFFIX" value=".jsp"/>
</map>
</property>
</bean>