Spring Boot整合Spring Security

前言:安全框架目前有兩大主流,一個是apache的Shiro,一個是Spring的Security, 曾經用過Shiro,又想看一下security和Shiro的不一樣,又加上Spring Boot能夠無縫對接Security,因此在此使用Security做爲安全組件。 安全框架主要功能爲:身份認證,權限控制,預防漏洞攻擊 因此接下來咱們圍繞若是配置身份認證,權限控制去整合Security。前端

環境: IDEA版本2017.3.1 x64, JDK1.8, SpringBoot2.1.1, Druid1.1.8, mybatis1.3.2,Security5.1.2,thymeleaf3.0.11算法

總流程:spring

  • 引入security的依賴包以及和security-thymeleaf的整合包
  • 實現MySecurityConfig配置類去定製咱們的受權規則和認證策略
  • 實現UserDetailsService認證策略用於MySecurityConfig類
  • 編寫一個Controller用於測試權限
  • 前端頁面限制角色訪問內容
  • 數據庫須要注意的問題

引入security的依賴包以及和security-thymeleaf的整合包

<!--引入security-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <!-- 引入security與thymeleaf的整合依賴 -->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
            <version>3.0.4.RELEASE</version>
        </dependency>
複製代碼

實現MySecurityConfig配置類去定製咱們的受權規則和認證策略

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class MySecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    MyUserDetailsService myUserDetailsService;

    //定製請求的受權規則
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("VIP1")
                .antMatchers("/level2/**").hasRole("VIP2")
                .antMatchers("/level3/**").hasRole("VIP3");
        
        /*開啓自動配置的登陸功能,若是是本身的定製的登入頁面,那麼/userlogin 的get請求是來到登陸頁面,/userlogin的post請求是處理認證登陸
        也就是loginPage中的URL的post請求是處理登陸邏輯的。沒登陸的時候,訪問會以get的方式訪問loginpage的URL來到登陸頁面*/
        http.formLogin().usernameParameter("username").passwordParameter("password").loginPage("/userlogin");
        
        //開啓自動配置的註銷功能,會訪問/logout請求
        http.logout().logoutSuccessUrl("/"); //註銷成功後,回到首頁
        
        /*開啓記住我功能(開啓後,springboot會給瀏覽器發送一個cookies,之後訪問網站都會帶上這個cookies給springboot驗證,springboot會檢查之前某一個用戶的cookies的值是什麼,若是找到了,這個用戶就不用再次登陸了,註銷時候springboot會發送命令給瀏覽器刪除cookies)*/
        http.rememberMe();
    }

    //定義認證規則
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
       //使用自定義認證規則,而且使用BCrypt算法處理密碼
       auth.userDetailsService(myUserDetailsService).passwordEncoder(new BCryptPasswordEncoder());
    }
}
複製代碼

主要就是配置受權規則和認證策略,認證策略咱們是連數據庫去校驗咱們的用戶和密碼,因此須要去實現一個UserDetailsService。數據庫

實現UserDetailsService認證策略用於MySecurityConfig

@Service
public class MyUserDetailsService implements UserDetailsService{

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    UserService userService;
    @Autowired
    HttpServletRequest request;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.selectUser(username);
        //logger.info(user.getName());
        if (user == null){
            throw new UsernameNotFoundException("用戶名不存在!");
        }
        HttpSession session = request.getSession();
        session.setAttribute("user",user);
        session.setAttribute("sessusername",username);

        List<GrantedAuthority> authorities = new ArrayList<>();

        //角色
        authorities.add(new SimpleGrantedAuthority(user.getRole()));

        //權限(爲了測試,硬編碼,實際上應該從數據庫中讀取)
        authorities.add(new SimpleGrantedAuthority("1"));

        logger.info(user.getName()+"角色權限爲:"+authorities.toString());

        return new org.springframework.security.core.userdetails.User(user.getName(),user.getPassword(),authorities);
    }
}
複製代碼

編寫一個Controller用於測試權限

@Controller
public class KungfuController {
	private final String PREFIX = "pages/";
	/**
	 * 歡迎頁
	 * @return
	 */
	@GetMapping("/")
	public String index() {
		return "welcome";
	}
	
	/**
	 * 登錄頁
	 * @return
	 */
	@GetMapping("/userlogin")
	public String loginPage() {
		return PREFIX+"login";
	}
	
	/**
	 * level1頁面映射
	 * @param path
	 * @return
	 */

    //@PreAuthorize("hasRole('VIP1') AND hasAuthority('1')")
    @PreAuthorize("hasAuthority('1')")
	@GetMapping("/level1/{path}")
	public String level1(@PathVariable("path")String path) {
		return PREFIX+"level1/"+path;
	}

}
複製代碼

後臺的內容就實現了,接下來看前端如何限制角色訪問內容apache

前端頁面限制角色訪問內容

登陸頁核心內容:

頁面需導入security和thymeleaf的整合標籤 xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"瀏覽器

div align="center">
		<form th:action="@{/userlogin}" method="post">
			用戶名:<input name="username"/><br>
			密碼:<input name="password"><br/>
            <p>
                <label for="remember-me">Remember Me?</label>
                <input type="checkbox" id="remember-me" name="remember-me"/>
            </p>
			<input type="submit" value="登錄">
		</form>
	</div>
複製代碼
主頁核心內容:
<div sec:authorize="!isAuthenticated()">
    <h2 align="center">遊客您好,若是想查看武林祕籍 <a th:href="@{/userlogin}">請登陸</a></h2>
</div>
<div sec:authorize="isAuthenticated()">
    <h2><span sec:authentication="name"></span>你好,你的角色爲:
        <span sec:authentication="principal.authorities"></span>
    </h2>
    <form th:action="@{/logout}" th:method="post">
        <input th:type="submit" th:value="註銷"/>
    </form>
</div>
<hr>

<div sec:authorize="hasRole('VIP1')">
    <h3>普通武功祕籍</h3>
    <ul>
        <li><a th:href="@{/level1/1}">羅漢拳</a></li>
        <li><a th:href="@{/level1/2}">武當長拳</a></li>
        <li><a th:href="@{/level1/3}">全真劍法</a></li>
    </ul>
</div>
複製代碼

數據庫實現

爲了測試,數據庫設計過於簡陋,但注意的問題爲角色字段的內容須要加上ROLE_前綴,不然會security會認爲此用戶依然沒有權限安全

密碼能夠編寫一個工具類進行加密 工具類以下:springboot

@Component
public class BCryptUtil {

    private BCryptPasswordEncoder bCryptPasswordEncoder;

    public BCryptUtil(){
         this.bCryptPasswordEncoder =  new BCryptPasswordEncoder();
    }

    /**
     * 加密
     * @return
     */
    public String encoder(String password){
        return bCryptPasswordEncoder.encode(password);
    }

    /**
     * 驗證密碼
     * @param password
     * @param salt
     * @return
     */
    public Boolean matches(String password,String salt){
        return  bCryptPasswordEncoder.matches(password,salt);
    }

}
複製代碼

整合就到此完畢,時間較晚,若是有錯漏盡管提出。bash

更多Spring Boot整合可瀏覽此博客:malizhi.cncookie

相關文章
相關標籤/搜索