Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow all URLs but one in Spring security

I would like to protect just a single URL, while allowing anonymous access for everything else.

The Java configuration examples i'm seeing in the internet all seem to indicate that you need to explicitly permitAll each and every URL, and appropriate hasRole for URLs that need to be protected. This in my case, creates a really unwieldy java code which I have modify every time I add a new URL to the application. Is there an easier java configuration that I can use.

And note also that in my case, the URL i'm protecting is a sub-resource, say employee/me, I would like employee/list, etc., to be anonymously accessible.

like image 733
Sathyakumar Seshachalam Avatar asked Apr 03 '16 07:04

Sathyakumar Seshachalam


People also ask

What is permitAll in Spring Security?

2. access=”permitAll” Setting up an <intercept-url> element with access=”permitAll” will configure the authorization so that all requests are allowed on that particular path: <intercept-url pattern="/login*" access="permitAll" /> Or, via Java configuration: http.

What is usage of @secured annotation?

Using @Secured Annotation. The @Secured annotation is used to specify a list of roles on a method. So, a user only can access that method if she has at least one of the specified roles.

How do I disable Defaultsecurityfilterchain?

In application. properties , set security. ignored=none .


1 Answers

If you're using Java Configuration, you can use something like following in your configure method:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
                .antMatchers("/employee/me").authenticated()
                .antMatchers("/**").permitAll();
}
like image 199
Ali Dehghani Avatar answered Oct 17 '22 10:10

Ali Dehghani