forked from lifangyu/springboot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurityConfig.java
More file actions
60 lines (51 loc) · 2.26 KB
/
Copy pathSecurityConfig.java
File metadata and controls
60 lines (51 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package demo.security;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
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.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
/**
* 安全配置文件,主要是重写默认的认证方式和访问目录权限
*
* @author jiekechoo
*
*/
@Configuration
@EnableWebSecurity
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
DataSource dataSource;
/**
* 使用jdbc认证方式,密码采用BCrypt加密,salt 10
*/
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(new BCryptPasswordEncoder(10));
}
@Configuration
@Order(1)
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/api/i/**").authorizeRequests().anyRequest().hasRole("USER").and().httpBasic().and().csrf() .disable();
}
}
@Configuration
@Order(2)
public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// http.httpBasic().disable();//启用redis需要启用此行
http.authorizeRequests()
.antMatchers( "/u/","/", "/assets/**", "/plugins/**", "/static/**", "/bootstrap/**",
"/api-docs/**", "/debug/**", "/api/**","/dist/**","/public/")
.permitAll().antMatchers("/admin/**").hasRole("ADMIN").anyRequest().authenticated().and()
.formLogin().loginPage("/login").permitAll().and().logout().permitAll().and().csrf().disable();
}
}
}