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")