I created this simple AdminController:
@Controller
@RequestMapping("admin")
public class AdminController {
@PreAuthorize("hasAuthority('ROLE_ADMIN')")
@RequestMapping(value = "/", method = RequestMethod.GET)
public @ResponseBody String welcomeAdmin() {
return "Spring Security - ROLE_ADMIN";
}
@PreAuthorize("hasAuthority('ROLE_ADMIN')")
@RequestMapping(value = "/{query}", method = RequestMethod.GET)
public @ResponseBody String welcomeAdmin(@PathVariable String query) {
return query;
}
}
This is the security-context.xml:
<http auto-config="true">
<intercept-url pattern="/admin*" access="ROLE_ADMIN"/>
<logout logout-success-url="/admin" />
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="user" password="password" authorities="ROLE_USER" />
<user name="admin" password="password" authorities="ROLE_ADMIN" />
</user-service>
</authentication-provider>
</authentication-manager>
Which is loaded here in the web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/root-context.xml
/WEB-INF/spring/appServlet/security-context.xml
</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
There are no errors, but the /admin resource is accessible to anyone, why is it that the resource is not being filtered by the Spring security?
what version of spring security are you using?
in your security-context.xml you need
in order to use annotations.
also you should try: