WebSecurityConfig
package com.chenmuxin.springdemo.configuration;
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;
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
// 返回一个URL拦截注册器
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/myLogin.html")
.loginProcessingUrl("/login")
// 使登陆页面不设限访问
.permitAll()
.and()
.csrf()
.disable();
}
}
SpringDemoApplication
package com.chenmuxin.springdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class SpringDemoApplication {
@GetMapping("/")
public String hello(){
return "Hello,spring securtiy";
}
public static void main(String[] args) {
SpringApplication.run(SpringDemoApplication.class, args);
}
}
application.properties
spring.security.user.name=root
spring.security.user.password=root