Answer1215

pom.xml:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

 

SecurityConfiguration.java:

package com.frankmoley.boot.essentials.initialbootapp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(final HttpSecurity http) throws Exception {

        http.authorizeRequests().antMatchers("/", "/api")
                // when it comes to / or /api/*, no need to check
                .permitAll()
                // any other reuqest should have authentication
                .anyRequest().authenticated()
            .and()
// if not authenticated, redirect to login form .formLogin()
// allow /login for form login .loginPage("/login") .permitAll() .and() // allow logout .logout() .permitAll(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { // not production code auth.inMemoryAuthentication() // just demo // in real world, use BCryptPasswordEncoder .passwordEncoder(NoOpPasswordEncoder.getInstance()) .withUser("user").password("password").roles("USER"); } }

 

分类:

技术点:

相关文章:

  • 2021-10-02
  • 2018-06-24
  • 2021-12-04
  • 2021-08-19
  • 2018-04-07
  • 2021-08-24
  • 2019-06-29
猜你喜欢
  • 2021-11-15
  • 2021-11-22
  • 2022-01-17
  • 2021-08-27
  • 2022-01-13
  • 2022-01-17
相关资源
相似解决方案