Yes you could.
To do this, you can assign a specific role to each user. For example, in your case, designate the user who owns the items as the column of the ADMIN role and all other ANONYMOUS or USER you choose. After that, using spring you can make the request unsuccessful for users who have the ANONYMOUS or USER role for the URL of the elements and allow users with the ADMIN role to view the elements.
This can now be achieved using spring security in several ways:
1. Using @PreAuthorize tags for individual controller methods and test roles ADMIN / USER / .. But, I think you do not want to change the controller as such.
A short manual way to create an authentication object in a context holder and use the spring security configuration to load, for example below:
@Order(1) public class UserFilter extends Filter { @Autowired UserService userService; ... UserObject userObject = userService.getUser(arg..); List<GrantedAuthority> grantedAuthorityList = new ArrayList<GrantedAuthority>(); grantedAuthorityList.add( new SimpleGrantedAuthority((userObject.getRoleName()));
And the security configuration class:
@Configuration @EnableWebSecurity public class SecurityConfigREST extends WebSecurityConfigurerAdapter { SecurityConfigREST(){ super(true); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { PreAuthenticatedAuthenticationProvider pap=new PreAuthenticatedAuthenticationProvider(); pap.setPreAuthenticatedUserDetailsService(new PreAuthenticatedGrantedAuthoritiesUserDetailsService()); auth.authenticationProvider(pap); } @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .regexMatchers("^/items.*$").hasAuthority("ROLE_ADMIN")
- Use the UserDetailsService in the security configuration above and load the user and his role into the authenticated authentication provider. See http://docs.spring.io/autorepo/docs/spring-security/3.2.2.RELEASE/apidocs/org/springframework/security/core/userdetails/UserDetailsService.html
Having said all this, itβs also a good design not to skip numbers (1,2,3) at the URL, which can lead to potential problems later, so use GET and pass the JSON request body to it, for example:
/items RequestMethod.GET { "itemList" : [1,2,3,4,5] } Hope that helps.
source share