Switch to the full version.
//相当于三个注解,以后再讲
@SpringBootApplication
//相当于ResponseBody 和 Controller
@RestController
//在这个类中所使用的jar包都会被加载,而且提供默认配置 excludeName可以取消默认配置
@EnableAutoConfiguration
@RequestMapping("/")
public String home(){
return "MackyHuang First SpringBoot";
}
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-tomcat</artifactId>-->
<!--<scope>provided</scope>-->
<!--</dependency>-->
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Resource
private UserServiceOwn serviceOwn;
@Override
protected void configure(HttpSecurity http) throws Exception {
//允许主目录 / 的访问
//check任何目录
//允许注销
//允许表单登陆
//禁用csrf
http.authorizeRequests()
.antMatchers("/authorize", "/").permitAll()
.anyRequest().authenticated()
.and()
.logout().permitAll()
.and()
.formLogin();
http.csrf().disable();
}
//允许资源文件加载
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/js/**", "/css/**", "/images/**");
}
//Spring Security中密码的存储格式需要加密,所以需要这种格式
//如果再数据库中
//需要
//auth.userDetailsService(userService).passwordEncoder(new BCryptPasswordEncoder());
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("macky")
.password(new BCryptPasswordEncoder().encode("123456"))
.roles("ADMIN");
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("huang")
.password(new BCryptPasswordEncoder().encode("123456"))
.roles("ADMIN");
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("user")
.password(new BCryptPasswordEncoder().encode("123456"))
.roles("USER");
//auth.userDetailsService(serviceOwn).passwordEncoder(new PasswordEncoderOwn());
////security默认的数据库操作
//auth.jdbcAuthentication().usersByUsernameQuery("macky").authoritiesByUsernameQuery("admin").passwordEncoder(new BCryptPasswordEncoder());
}
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/authorize", "/").permitAll() //允许主目录 / 的访问
.anyRequest().authenticated() //check任何目录
.and()
.logout().permitAll() //允许注销
.and()
.formLogin(); //允许表单登陆
http.csrf().disable(); //禁用csrf
}
// 允许资源文件加载
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/js/**", "/css/**", "/images/**");
}
//这里只介绍关于内存中的储存用户信息
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//Spring Security中密码的存储格式需要加密,所以需要这种格式
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("macky")
.password(new BCryptPasswordEncoder().encode("123456"))
.roles("ADMIN");
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("huang")
.password(new BCryptPasswordEncoder().encode("123456"))
.roles("ADMIN");
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("user")
.password(new BCryptPasswordEncoder().encode("123456"))
.roles("USER");
。。其实以上的内容,就是内存中创建一个用户信息,指定气密码的匹配器,然后指定用户名,密码和角色,这里我们创建了3个用户,俩个角色
//auth.userDetailsService(serviceOwn).passwordEncoder(new PasswordEncoderOwn());
////security默认的数据库操作
//auth.jdbcAuthentication().usersByUsernameQuery("macky").authoritiesByUsernameQuery("admin").passwordEncoder(new BCryptPasswordEncoder());
}
搞定了用户角色层次的,现在我们来配置一些访问的网页把(以下代码都在XXXApplication中,这里我们其实是把它当作是一个Controller,因为注解里面已经将它进行了配置)
@RequestMapping("/hello")
public String hello(){
return "hello world";
}
@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping("/manage")
public String manage(){
return "Only admin can see this page";
}
@EnableGlobalMethodSecurity(prePostEnabled = true)
@PreAuthorize("hasRole('ROLE_ADMIN')")
@EnableGlobalMethodSecurity
就是使得上面的这个注解生效
其实类似于 @PreAuthorize
这样的注解不止这一个
// 这是方法进入前的判断,可以有内置的方法,也可以对参数进行判断
@PreAuthorize("#index<10")
// 拦截方法调用后 这里还是遭到了拦截
@PostAuthorize("returnObject==2")
// 如果参数或者返回值是集合的时候,就可以使用*Filter注解,功能和上面的是一样的
// filterObject表示集合内的一个元素
@PreFilter("filterObject<10")
@PostFilter("filterObject<5")