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()); }
@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
这样的注解不止这一个css
// 这是方法进入前的判断,能够有内置的方法,也能够对参数进行判断 @PreAuthorize("#index<10") // 拦截方法调用后 这里仍是遭到了拦截 @PostAuthorize("returnObject==2") // 若是参数或者返回值是集合的时候,就可使用*Filter注解,功能和上面的是同样的 // filterObject表示集合内的一个元素 @PreFilter("filterObject<10") @PostFilter("filterObject<5")