Currently I am using apache shiro authentication in my spring boot project. I wrote Custom Realm for Cassandra DB. While autowiring the class inside realm object returns null when submitting login details. My Application Config(@component annotation used):
@Bean(name = "realm")
@DependsOn("lifecycleBeanPostProcessor")
public ApplicationRealm realm() {
ApplicationRealm realm = new ApplicationRealm();
realm.init();
return realm;
}
@Bean
public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
return new LifecycleBeanPostProcessor();
}
My Application Realm Class :
@Configuration
@ComponentScan("com.scm.auth")
public class ApplicationRealm extends AuthorizingRealm {
@Autowired
IAuthRepository authRepo;
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
Set<String> roles = new HashSet<String>();
try {
roles.add("admin");
} catch (Exception rEx) {
throw new AuthorizationException(rEx);
}
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roles);
info.setRoles(roles);
return info;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
SimpleAuthenticationInfo info = null;
UsernamePasswordToken upToken = (UsernamePasswordToken) token;
User user = authRepo.findByUserName(upToken.getUsername(), true); // my model class
try {
if (user.getCurrentPwd().equals(upToken.getPassword())) {
info = new SimpleAuthenticationInfo(user, user.getCurrentPwd(), getName());
} else {
throw new AuthenticationException("Login name [" + upToken.getUsername() + "] not found!");
}
} catch (Exception idEx) {
throw new AuthenticationException(idEx);
}
return info;
}
Is any annotation missed?
Seems like the you have configured the mismatched annotation. Your ApplicationRealm.java should no have @Configuration annotation. @Component is enough for this Custome Realm.
/*@Configuration*/
@ComponentScan("com.scm.auth")
public class ApplicationRealm extends AuthorizingRealm{
/**/
}