Category Archives: Java

JSF 2.2 ViewScoped

JSF 2.2 has the missing ViewScoped annotation working with CDI. Simply use @javax.faces.view.ViewScoped on your bean. Be careful NOT to mix up with @javax.faces.bean.ViewScoped.

Example view scoped class:

import javax.faces.view.ViewScoped;
import javax.inject.Named;

@ViewScoped
@Named
public class SenderView implements Serializable {
}

If you use the wrong annotation you will get weld errors when trying to inject an instance of the class.

JBoss Maven Repository

To use the jboss maven repository under https://repository.jboss.org/nexus/ you have to add a profile in your settings.xml (~/.m2 or $M2_HOME/conf).

<settings>  
  ...  
  <profiles>  
    ...  
    <profile>  
      <id>jboss-public-repository</id>  
      <repositories>  
        <repository>  
          <id>jboss-public-repository-group</id>  
          <name>JBoss Public Maven Repository Group</name>  
          <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>  
          <layout>default</layout>  
          <releases>  
            <enabled>true</enabled>  
            <updatePolicy>never</updatePolicy>  
          </releases>  
          <snapshots>  
            <enabled>true</enabled>  
            <updatePolicy>never</updatePolicy>  
          </snapshots>  
        </repository>  
      </repositories>  
      <pluginRepositories>  
        <pluginRepository>  
          <id>jboss-public-repository-group</id>  
          <name>JBoss Public Maven Repository Group</name>  
          <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>  
          <layout>default</layout>  
          <releases>  
            <enabled>true</enabled>  
            <updatePolicy>never</updatePolicy>  
          </releases>  
          <snapshots>  
            <enabled>true</enabled>  
            <updatePolicy>never</updatePolicy>  
          </snapshots>  
        </pluginRepository>  
      </pluginRepositories>  
    </profile>  
  
  </profiles>  
  
  <activeProfiles>  
    <activeProfile>jboss-public-repository</activeProfile>  
  </activeProfiles>  
  ...  
</settings>

[copied from https://developer.jboss.org/wiki/MavenGettingStarted-Users, 25.02.2015]

Of course you have to activate it.

SSL in Wildfly 8.2.0

Since wildfly uses undertow the configuration of SSL has changed. You can follow these steps to get SSL running:

  • Create a keystore with keys:
cd $WILDFLY_HOME/standalone/configuration
keytool -genkey -alias localhost -keyalg RSA -keystore keystore.jks -keysize 4096

You need to replace “localhost” with your domain name.

NOTE: Your browser will complain that the connection is unsecure because we have no officially signed certificate. Look for a tutorial to create a keypair and a certificate sign request (csr) that you have to send to a certification authority (ca).

  • Configure the SslRealm:
<management>
  <security-realms>
...
    <security-realm name="SslRealm">
      <server-identities>
        <ssl>
          <keystore path="keystore.jks" relative-to="jboss.server.config.dir" keystore-password="changeme"/>
        </ssl>
      </server-identities>
    </security-realm>
...
  </security-realms>
...
  •  And add a listener:
<subsystem xmlns="urn:jboss:domain:undertow:1.2">
  <buffer-cache name="default"/>
  <server name="default-server">
    <http-listener name="default" socket-binding="http"/>
    <https-listener name="default-ssl" socket-binding="https" security-realm="SslRealm"/>

Now you should be able to access your wildfly under https://localhost:8433/.

Widlfly 8.2.0 – JBAS010153: Node identifier is set to the default value

In the latest version of Wildfly (8.2.0) there is a warning looking like

[org.jboss.as.txn] (ServerService Thread Pool -- 46) JBAS010153: Node identifier property is set to the default value. Please make sure it is unique.

To get rid of this you have to change the standalone.xml file.  Change this part

<subsystem xmlns="urn:jboss:domain:transactions:2.0">
  <core-environment>
    <process-id>
      <uuid/>
    </process-id>
  </core-environment>
  <recovery-environment socket-binding="txn-recovery-environment" status-socket-binding="txn-status-manager"/>
</subsystem>

to look like this

<subsystem xmlns="urn:jboss:domain:transactions:2.0">
  <core-environment node-identifier="wildfly1">
    <process-id>
      <uuid/>
    </process-id>
  </core-environment>
  <recovery-environment socket-binding="txn-recovery-environment" status-socket-binding="txn-status-manager"/>
</subsystem>

Of course you can use any value as identifier and “wildfly1” is just an example. Make sure not to use more than 23 characters.

Encrypted Database Passwords in JBoss

JBoss provides a simple mechanism to encrypt database passwords with blowfish. So the standalone.xml does not include our database passwords in plaintext anymore.

First you have to encrypt your password with org.picketbox.datasource.security.SecureIdentityLoginModule. This class includes a main method so you can run it with a single argument which has to be your plaintext password. The result will look like this:

Encoded password: 1ab234cf321cca

The class is included in jboss modules.

Then create a security-domain in your standalone.xml file:

<security-domain name="databaseSecure" cache-type="default">
  <authentication>
    <login-module code="org.picketbox.datasource.security.SecureIdentityLoginModule" flag="required">
      <module-option name="username" value="username"/>
      <module-option name="password" value="1ab234cf321cca"/>
    </login-module>
  </authentication>
</security-domain>

Or with cli:

/subsystem=security/security-domain=databaseSecure:add(cache-type=default)  
/subsystem=security/security-domain=databaseSecure/authentication=classic:add(login-modules=[{"code"=>"org.picketbox.datasource.security.SecureIdentityLoginModule", "flag"=>"required", "module-options"=>[("username"=>"username"), ("password"=>"1ab234cf321cca")]}])

 

The last step is to replace the username+password part of your datasource with a security-domain element. This would look like this in its simplest way:

<datasource jndi-name="java:jboss/datasources/mypgDS" pool-name="MypgDS" enabled="true" use-java-context="true">
  <connection-url>jdbc:postgresql:db1</connection-url>
  <driver>postgresql</driver>
  <security>
    <security-domain>databaseSecure</security-domain>
  </security>
</datasource>

After theses changes start your application server.

ATTENTION! The passphrase that is used for the Blowfish algorithm is hardcoded in the login module. To make this secure you have to change the password in that component. Change the source and recompile or create an extension and overwrite all necessary parts and add it as a new module.

Disable Stacktrace in Response of JBoss

To disable the source body in a stacktrace you have to add the following configuration to your web subsystem in standalone.xml:

<configuration>
  <jsp-configuration display-source-fragment="false"/>
</configuration>

Or with cli:

/subsystem=web/configuration=jsp-configuration:write-attribute(name=display-source-fragment, value=false)