1- // package com.us.example.config;
2- //
3- // import org.springframework.context.annotation.Configuration;
4- // import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
5- // import org.springframework.security.config.annotation.web.builders.HttpSecurity;
6- // import org.springframework.security.config.annotation.web.builders.WebSecurity;
7- // import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
8- // import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
9- //
10- // @Configuration
11- // @EnableWebSecurity
12- // public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
13- // @Override
14- // protected void configure(HttpSecurity http) throws Exception {
15- // http
16- // .authorizeRequests()
17- // .antMatchers("/","/login").permitAll()//1根路径和 /login路径不拦截
18- // .anyRequest().authenticated()
19- // .and()
20- // .formLogin()
21- // .loginPage("/login") //2登陆页面
22- // .defaultSuccessUrl("/chat") //3登陆成功转向该页面
23- // .permitAll()
24- // .and()
25- // .logout()
26- // .permitAll();
27- // }
28- //
29- // //4
30- // @Override
31- // protected void configure(AuthenticationManagerBuilder auth) throws Exception {
32- // auth
33- // .inMemoryAuthentication()
34- // .withUser("wyf").password("wyf").roles("USER")
35- // .and()
36- // .withUser("wisely").password("wisely").roles("USER");
37- // }
38- // //5忽略静态资源的拦截
39- // @Override
40- // public void configure(WebSecurity web) throws Exception {
41- // web.ignoring().antMatchers("/resources/static/**");
42- // }
43- //
44- // }
1+ package com .us .example .config ;
2+
3+ import org .springframework .context .annotation .Configuration ;
4+ import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
5+ import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
6+ import org .springframework .security .config .annotation .web .builders .WebSecurity ;
7+ import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
8+ import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
9+
10+ @ Configuration
11+ @ EnableWebSecurity
12+ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
13+ @ Override
14+ protected void configure (HttpSecurity http ) throws Exception {
15+ http
16+ .authorizeRequests ()
17+ .antMatchers ("/" ,"/login" ).permitAll ()//根路径和 /login路径不拦截
18+ .anyRequest ().authenticated ()
19+ .and ()
20+ .formLogin ()
21+ .loginPage ("/login" ) //2登陆页面路径为/login
22+ .defaultSuccessUrl ("/chat" ) //3登陆成功转向chat页面
23+ .permitAll ()
24+ .and ()
25+ .logout ()
26+ .permitAll ();
27+ }
28+
29+ //4在内存中配置两个用户 wyf 和 wisely ,密码和用户名一致,角色是 USER
30+ @ Override
31+ protected void configure (AuthenticationManagerBuilder auth ) throws Exception {
32+ auth
33+ .inMemoryAuthentication ()
34+ .withUser ("wyf" ).password ("wyf" ).roles ("USER" )
35+ .and ()
36+ .withUser ("wisely" ).password ("wisely" ).roles ("USER" );
37+ }
38+ //5忽略静态资源的拦截
39+ @ Override
40+ public void configure (WebSecurity web ) throws Exception {
41+ web .ignoring ().antMatchers ("/resources/static/**" );
42+ }
43+
44+ }
0 commit comments