bkuchcik / cours-jee-5-scope

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cours sur les scopes

Pour vous aider avec Spring Security suivez le guide de paramétrage

Avertissement

Dans le build, j'ai ajouté un dépendance à Spring Security qui n'est disponible que lorsque vous utiliser les tp.

Le nom du module doit contenir tp-display-quote pour obtenir cette dépendance (vous le verrez dans la correction).

Activer spring security

Pour activer Spring Security il faut rajouter la dépendance suivante :

implementation("org.springframework.boot:spring-boot-starter-security")

Voici une classe de configuration de base de Spring Security en Java

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/resources/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .successHandler((request, response, authentication) -> {
                    request.getSession().setAttribute("username", authentication.getName());
                    response.sendRedirect("/main");
                }).permitAll();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .inMemoryAuthentication()
                .withUser(User.withDefaultPasswordEncoder().username("grut").password("APass").roles("USER"))
                .withUser(User.withDefaultPasswordEncoder().username("admin").password("admin").roles("ADMIN"));
    }
}

La version équivalante en Kotlin

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/resources/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .successHandler((request, response, authentication) -> {
                    request.getSession().setAttribute("username", authentication.getName());
                    response.sendRedirect("/main");
                }).permitAll();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .inMemoryAuthentication()
                .withUser(User.withDefaultPasswordEncoder().username("grut").password("APass").roles("USER"))
                .withUser(User.withDefaultPasswordEncoder().username("admin").password("admin").roles("ADMIN"));
    }
}

Ces classes vous permettront de gérer la sécurité avec SpringSecurity, de créer des uses

About


Languages

Language:Kotlin 46.3%Language:Java 46.0%Language:JavaScript 6.1%Language:CSS 1.6%