Java annotation based Spring security config

Arefe

I try to do the Java annotation based Spring security configuration. I do this after following a tutorial and have the code as provided,

@Configuration
@EnableWebSecurity
// need to change this to the security directory
@ComponentScan("")
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private RestAuthenticationEntryPoint restAuthenticationEntryPoint;

    @Autowired
    private MySavedRequestAwareAuthenticationSuccessHandler authenticationSuccessHandler;

    @Override
    protected void configure(AuthenticationManagerBuilder auth)
            throws Exception {

        auth.inMemoryAuthentication()
                .withUser("temporary").password("temporary").roles("ADMIN")
                .and()
                .withUser("user").password("userPass").roles("USER");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .exceptionHandling()
                .authenticationEntryPoint(restAuthenticationEntryPoint)
                .and()
                .authorizeRequests()
                .antMatchers("/api/foos").authenticated()
                .and()
                .formLogin()
                .successHandler(authenticationSuccessHandler)
                .failureHandler(new SimpleUrlAuthenticationFailureHandler())
                .and()
                .logout();
    }

    @Bean
    public MySavedRequestAwareAuthenticationSuccessHandler mySuccessHandler() {
        return new MySavedRequestAwareAuthenticationSuccessHandler();
    }

    @Bean
    public SimpleUrlAuthenticationFailureHandler myFailureHandler() {
        return new SimpleUrlAuthenticationFailureHandler();
    }
}

The API base for the project I work,

public static final String API_BASE = "/*";

For example, I do the cURL request like,

curl -X GET http://localhost:8080/rest/wallet/wallets | json

I'm not sure about the .antMatchers("/api/foos").authenticated() line in the code. For example, from where the foos is coming and do I need to change it to something like .antMatchers("/foos").authenticated()?

Karthik R

If you are new to programming, its a valid question. But get used to it. All the examples would usually have 'foo' and 'bar' as sample variables, method names etc.

Anyways, the .antMatchers("/api/foos").authenticated() specifies that the pattern URL that matches /api/foo need to be authenticated and then the following handlers should be used.

Change the pattern to your matching one - .antMatchers("/rest/wallet/**") and test your code.

For more reference - read this post : When to use Spring Security`s antMatcher()?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert spring bean integration From XML to Java annotation based Config

spring security: NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.config.annotation.ObjectPostProcessor] found

Spring Security Java Config not generating logout url

Spring Security, Method Security annotation (@Secured ) is not working (java config)

Spring Security Java Config

Spring Annotation Configs - dynamically create classes based on YAML config

Spring security: Authentication manager and global security config with Java config from xml config

Java Config for Spring Security with Vaadin

Spring Security OAuth Java Config

Spring security - Custom AuthenticationProvider not working - Java Config

Spring Security XML Config Vs Java Config

Spring Security Java Config - custom AuthenticationProvider and UserDetailsService

Spring Security Java Config for LDAP

Java Spring Security config - multiple authentication providers

Spring security kerberos works with xml config but not with Java config

Spring Security Java Config no ContextLoaderListener registered

Spring Security: Not able to configure method security with java config

Neo4J - Spring Boot - Spring Security - Java Config

spring security java based config after tomcat restart

Spring Batch Java Config @EnableBatchProcessing Annotation Error

Spring Security (Java Config) Login - Return Different URL Based on User Role

Spring MVC xml based config to annotation

Spring Security login with UserDetailsService and Java config

spring security config xml to java

Spring Java based config "chicken and Egg situation"

Spring MVC Java based config - JSP not resolving

How to correctly define web.xml and java-based config for Spring Security at the same time

Integrate XML based spring mvc with java config based spring security

Spring java annotation-based config won't work, but xml-based config does?