Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Differences between code and article in the Spring Security setup #5

Open
mxmlnglt opened this issue Sep 8, 2021 · 0 comments
Open

Comments

@mxmlnglt
Copy link

mxmlnglt commented Sep 8, 2021

I read https://www.baeldung.com/spring-security-oauth2-authentication-with-reddit in order to understand how to implement OAuth in a legacy Spring (no Boot, Spring Security 4.x) web application but seems like the code doesn't match the article.

esp.

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    /* ... */

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .anonymous().disable()
            .csrf().disable()
            .authorizeRequests()
            .antMatchers("/home.html").hasRole("USER")
            .and()
            .httpBasic()
            .authenticationEntryPoint(oauth2AuthenticationEntryPoint());
    }

    private LoginUrlAuthenticationEntryPoint oauth2AuthenticationEntryPoint() {
        return new LoginUrlAuthenticationEntryPoint("/login");
    }
}

which makes use of .authenticationEntryPoint() method + a LoginUrlAuthenticationEntryPoint object,

while https://github.com/Baeldung/reddit-app/blob/master/reddit-web/src/main/java/org/baeldung/config/web/GeneralSecurityConfig.java

@Configuration
@EnableWebSecurity
@ComponentScan({ "org.baeldung.security" })
public class GeneralSecurityConfig extends WebSecurityConfigurerAdapter {

    /* ... */

    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        // @formatter:off
        http.anonymous().disable().csrf().disable().authorizeRequests()
            .antMatchers("/api/*","/feedForm","/profile","/scheduledPosts","/feeds","/changePassword","/updatePassword").authenticated()
            .antMatchers("/adminHome","/users").hasAuthority("USER_READ_PRIVILEGE")
        .and()
	    .formLogin().loginPage("/").loginProcessingUrl("/j_spring_security_check").defaultSuccessUrl("/home")
            .failureUrl("/?error").usernameParameter("username").passwordParameter("password")
            .successHandler(successHandler)
            .and()
            .logout().invalidateHttpSession(true).deleteCookies("JSESSIONID").logoutUrl("/logout").logoutSuccessUrl("/")
            .and()
            .sessionManagement().sessionFixation().none().maximumSessions(1).maxSessionsPreventsLogin(true);
        // @formatter:on
    }

}

calls .formLogin() loginPage() failureUrl() and so on...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant