JforJava
JforJava

Reputation: 23

Integrate Multiple Shiro Realms into a Spring Boot environment Java

I have a xml based Apache Shiro SecurityManager and few custom realms (They extend build in realms). I am trying to migrate to Spring Boot, which is mostly annotation based.

I want to configure ALL realms into the security manager easily.Currently, I am able to do it by creating a ShiroConfig.java (annotated by @Configuration) , manually creating the object of each realm in the ShiroConfig and adding it under DefaultWebSecurityManager.setRealms(Collection).

Is there any other way, by which same thing can be achieved by annotation, where I annotate each Realm ,saying it's a realm and all of them will be added under security manager at runtime ?

@Bean(name="securityManager")

public DefaultWebSecurityManager securityManager() {
    DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
    manager.setRealm(userRealm());
    manager.setSessionManager(defaultWebSessionManager());
    return manager;
}

@Bean @DependsOn(value="lifecycleBeanPostProcessor")

public Realm_1 userRealm() {
    Realm_1 userRealm = new Realm_1();
    return userRealm;
}

Upvotes: 1

Views: 941

Answers (1)

Brian Demers
Brian Demers

Reputation: 2080

Shiro 1.4 is hot off the presses, we are working on getting the site updated now. But take a look at the examples, you can just inject your Realms in Spring and Spring-Boot.

https://github.com/apache/shiro/blob/master/support/spring/src/main/java/org/apache/shiro/spring/config/ShiroConfiguration.java#L44-L48

https://github.com/apache/shiro/blob/master/samples/spring/src/main/java/org/apache/shiro/samples/spring/CliApp.java#L46-L56

Upvotes: 0

Related Questions