I am building a REST API using Spring Boot with Shiro for authentication. I am getting a NullPointerException when I try to Autowire a repository in my realm code. Autowiring the repository elsewhere does not produce this error.
Here is my authorizing realm:
@Component
public class CCDAuthorizingRealm extends AuthorizingRealm {
@Autowired
private UserRepository userRepository;
public CCDAuthorizingRealm() { }
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
AuthenticationInfo authenticationInfo = null;
UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
String username = token.getUsername();
User user = null;
try {
user = userRepository.findByUsername(username);
} catch (Exception e) {
e.printStackTrace(System.err);
}
if (user != null) {
authenticationInfo = new SimpleAuthenticationInfo(
new SimplePrincipalCollection(username, username),
user.getPassword());
}
return authenticationInfo;
}
....
}
The exception occurs when I try to call the findByUsername() method.
Here is the code for the repository that I am injecting.
@Transactional(readOnly=true)
@RepositoryRestResource(collectionResourceRel="users", path="users")
public interface UserRepository extends PagingAndSortingRepository<User, Long> {
User findByUsername(@Param("user") String user);
....
}
To test, I autowired the repository into my Application class and printed the results, and I got no errors. For completeness, here is the code to show that:
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
public UserRepository userRepository;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... strings) {
System.out.println("\nServer up\n");
User user = userRepository.findOne(new Long(1));
System.out.println(user.getUsername());
}
}
Any help is greatly appreciated.
EDIT
As far as I can tell this question is not a duplicate, and the answer to the other question does not resolve my problem. I am injecting the bean and informing the IoC container of the component, but the autowiring still results in a NPE.
Using scopes does not help, because I only need the single service object. The Configurable tag is not necessary, because I am autowiring and do not need to create a new object.
UPDATE
Here is where I am calling CCDAuthorizingRealm. I'm new to using Shiro, but I'm pretty sure this is correct.
@RestController
@RequestMapping(produces="application/json")
public class AuthenticationController {
CCDAuthorizingRealm realm = new CCDAuthorizingRealm();
SecurityManager sm = new DefaultSecurityManager(realm);
public AuthenticationController() { }
/**
* Authenticate user with supplied credentials
*
* @param login - credentials
* @return success or failure message
*/
@RequestMapping(value = "/login", method = RequestMethod.POST, consumes="application/json")
public String login(@RequestBody LoginInfo login) {
String response;
UsernamePasswordToken token = new UsernamePasswordToken(
login.getUsername(),
login.getPassword(),
false);
try {
SecurityUtils.setSecurityManager(sm);
Subject currentUser = SecurityUtils.getSubject();
if (currentUser.isAuthenticated()) {
currentUser.logout();
}
currentUser.login(token);
response = token.toString();
} catch (AuthenticationException e) {
response = "Error: Incorrect username or password";
}
return response;
}
}
I currently try creating a new instance of my realm in the controller. I do this because when I try:
@Autowired
CCDAuthorizingRealm realm;
SecurityManager sm = new DefaultSecurityManager(realm);
I would get a runtime error when initializing the Security Manager because it would complain about realm being null. I don't understand why realm is null, however, since it is annotated as a component. Perhaps this is the root of my problem.