use stormpath group as spring boot object

35 views Asked by At

Can you use your Stormpath group object(i.e. ADMIN) as a class in your Spring Boot application? I mean, I want to keep some state of this object and I do not want to keep a correspondent POJO, which would have the ADMIN's email and password, plus another data like a gender/birthday, but simply have the whole state and behaviour in a instance of the stormpath group ADMIN.

Can this be achieved?

1

There are 1 answers

0
Les Hazlewood On

When you say use your Stormpath group object 'as a class', I assume you mean 'as a Spring bean'? If so, then most definitely!

For example:

import com.stormpath.sdk.client.Client;
import com.stormpath.sdk.group.Group;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

// ...

@Autowired
private Client client;

//I just made this property up - use any property name you want and
//define it in your Spring config and ensure the value is the 
//fully qualified Stormpath href corresponding to your admin group:
@Value("${groups.admin.href}")
private String adminGroupHref;

@Bean
public Group adminGroup() {
    return client.getResource(adminGroupHref, Group.class);
}

Then, elsewhere in your Spring config you can use adminGroup() to reference the group (in the save Java configuration class file). Or, if you want to reference the Group as a bean in other Java configuration classes or @Component beans, just autowire it:

@Autowired
private Group adminGroup;

However, if you define more than one Group bean in your Spring Java config (maybe other groups? like users or employees?), then you'll need a @Qualifier when autowiring to ensure that the correct Group instance is injected:

@Autowired
@Qualifier("adminGroup")
private Group adminGroup;

All Stormpath resource instances (Account, Group, Application, etc) are thread-safe, making them safe to be used as singletons if desired.