Spring Boot如何使用Spring Security進行安全控制
阿新 • • 發佈:2019-01-08
包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
package com.zhonghuan.bookkeeping.security; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; 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.core.userdetails.UserDetailsService; @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { // 進行使用者驗證 ,將其用@Bean註解交給spring管理 然後返回 查詢資料 的方法 @Bean UserDetailsService customUserService() { return new CustomUserService(); } // 重寫驗證的方法,將上面的方法Service 交給他下面設定 // configureGlobal(AuthenticationManagerBuilder auth)方法,在記憶體中建立了一個使用者 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(customUserService()); } //允許跨域 // @Bean // public WebMvcConfigurer corsConfigurer() { // return new WebMvcConfigurerAdapter() { // @Override // public void addCorsMappings(CorsRegistry registry) { // registry.addMapping("/**").allowedOrigins("*") // .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS") // .allowCredentials(false).maxAge(3600); // } // }; // } /** * permitAll配置例項 */ // configure(HttpSecurity http) 方法 // 通過 authorizeRequests() 定義哪些URL需要被保護、哪些不需要被保護。 @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable().authorizeRequests() // 不攔截的請求路徑 (放行) // permitAll沒有繞過spring security,其中包含了登入的以及匿名的。 // ingore是完全繞過了spring security的所有filter,相當於不走spring security .antMatchers("/getSmsCode","/regist").permitAll() .anyRequest().authenticated() // 通過 formLogin() 定義當需要使用者登入時候,轉到的登入頁面。 .and().formLogin() // 使用者的密碼和使用者 需要和 from 表單的 name屬性相同 .passwordParameter("password") .usernameParameter("user") // 登入頁面 .loginPage("/loginPage.html") // from表單提交的請求路徑 .loginProcessingUrl("/toLogin") // 登入成功後的跳轉 .defaultSuccessUrl("/welcome.html") .permitAll() .and().logout().permitAll(); } // @Override // public void configure(WebSecurity web) throws Exception { // //解決靜態資源被攔截的問題 // web.ignoring().antMatchers("/css/**"); // } }
web ignore配置例項 permitAll沒有繞過spring security,其中包含了登入的以及匿名的。 ingore是完全繞過了spring security的所有filter,相當於不走spring security @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers("/css/**"); web.ignoring().antMatchers("/js/**"); web.ignoring().antMatchers("/fonts/**"); } }
另外一個類用做查詢資料
package com.zhonghuan.bookkeeping.security; import com.zhonghuan.bookkeeping.entity.User; import com.zhonghuan.bookkeeping.login.dao.UserDao; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import javax.annotation.Resource; import java.util.ArrayList; public class CustomUserService implements UserDetailsService { @Resource private UserDao userDao; @Override public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException { User user = userDao.findByTellphone(userName); if (user == null) { throw new UsernameNotFoundException("使用者名稱不存在"); } return new org.springframework.security.core.userdetails.User(user.getTellphone(), user.getPassword(), new ArrayList<>()); } }