After studying many examples, I can't find any example that shows how create Spring Security configurations, while Roles are listed in annotations and Hibernate is used for authentication.
My files:
mvc-dispather-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<import resource="classpath:hibernate-beans.xml" />
<mvc:annotation-driven/>
<context:annotation-config/>
<context:component-scan base-package="com.salespredict"/>
</beans>
spring-security.xml:
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<http>
<http-basic/>
</http>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="authenticationService" />
</authentication-manager>
<global-method-security secured-annotations="enabled" />
</beans:beans>
Service:
@Service public class AuthenticationService implements UserDetailsService {
@Autowired
private IUserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findOne(username);
Set<Role> roles = user.getRoles();
Set<GrantedAuthority> authorities = new HashSet<>();
for(Role role:roles) {
authorities.add(new SimpleGrantedAuthority(role.getRole().name()));
}
return new org.springframework.security.core.userdetails.User(
user.getUsername(),
user.getPassword(),
authorities);
}
}
Controller:
@Controller
@Secured({RoleNames.ADMIN, RoleNames.SALES_PREDICT_ADMIN})
@RequestMapping("/admin")
public class Admin extends WebServiceBase {
@RequestMapping(value = "/users", method = RequestMethod.PUT, produces = "application/json", consumes = "application/json")
public
@ResponseBody
ResponseEntity registerNewUsers(InputStream data) throws Exception {
// deserialize from JSON
Users users = _mapper.readValue(data, Users.class);
PutUsers msg = new PutUsers(users.getUsers());
postMessage(msg, DefaultResponse.class);
return ok();
}
...
}
If I change <http>
to
<http use-expressions="true">
<intercept-url pattern="/**" access="isAuthenticated()" />
<http-basic />
</http>
Then my authentication service is called, but it just checks whether a user provides a password, it does not check roles. If I remove it, authentication servic is not called at all.
What should I write in <intercept-url pattern="/**" access= ... >
to make it checking roles from @Secured annotation?
Try to move your
declaration to
mvc-dispather-servlet.xml
because yourAdmin
controller is picked up bymvc-dispather-servlet.xml
and not byspring-security.xml
. See corresponding FAQ entry.