Spring Boot整合Spring Security

前言:安全框架目前有兩大主流,一個(gè)是apache的Shiro,一個(gè)是Spring的Security,曾經(jīng)用過Shiro,又想看一下security和Shiro的不同,又加上Spring Boot可以無縫對接Security,所以在此使用Security作為安全組件。
安全框架主要功能為:身份認(rèn)證,權(quán)限控制,預(yù)防漏洞攻擊
所以接下來我們圍繞如果配置身份認(rèn)證,權(quán)限控制去整合Security。

環(huán)境: IDEA版本2017.3.1 x64, JDK1.8, SpringBoot2.1.1, Druid1.1.8, mybatis1.3.2,Security5.1.2,thymeleaf3.0.11

總流程:

  • 引入security的依賴包以及和security-thymeleaf的整合包
  • 實(shí)現(xiàn)MySecurityConfig配置類去定制我們的授權(quán)規(guī)則和認(rèn)證策略
  • 實(shí)現(xiàn)UserDetailsService認(rèn)證策略用于MySecurityConfig類
  • 編寫一個(gè)Controller用于測試權(quán)限
  • 前端頁面限制角色訪問內(nèi)容
  • 數(shù)據(jù)庫需要注意的問題

一、引入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>

二、實(shí)現(xiàn)MySecurityConfig配置類去定制我們的授權(quán)規(guī)則和認(rèn)證策略

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

    @Autowired
    MyUserDetailsService myUserDetailsService;

    //定制請求的授權(quán)規(guī)則
    @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請求是處理認(rèn)證登錄
        也就是loginPage中的URL的post請求是處理登錄邏輯的。沒登錄的時(shí)候,訪問會以get的方式訪問loginpage的URL來到登錄頁面*/
        http.formLogin().usernameParameter("username").passwordParameter("password").loginPage("/userlogin");
        
        //開啟自動配置的注銷功能,會訪問/logout請求
        http.logout().logoutSuccessUrl("/"); //注銷成功后,回到首頁
        
        /*開啟記住我功能(開啟后,springboot會給瀏覽器發(fā)送一個(gè)cookies,以后訪問網(wǎng)站都會帶上這個(gè)cookies給springboot驗(yàn)證,springboot會檢查以前某一個(gè)用戶的cookies的值是什么,如果找到了,這個(gè)用戶就不用再次登錄了,注銷時(shí)候springboot會發(fā)送命令給瀏覽器刪除cookies)*/
        http.rememberMe();
    }

    //定義認(rèn)證規(guī)則
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
       //使用自定義認(rèn)證規(guī)則,并且使用BCrypt算法處理密碼
       auth.userDetailsService(myUserDetailsService).passwordEncoder(new BCryptPasswordEncoder());
    }
}

主要就是配置授權(quán)規(guī)則和認(rèn)證策略,認(rèn)證策略我們是連數(shù)據(jù)庫去校驗(yàn)我們的用戶和密碼,所以需要去實(shí)現(xiàn)一個(gè)UserDetailsService。

三、實(shí)現(xiàn)UserDetailsService認(rèn)證策略用于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()));

        //權(quán)限(為了測試,硬編碼,實(shí)際上應(yīng)該從數(shù)據(jù)庫中讀?。?        authorities.add(new SimpleGrantedAuthority("1"));

        logger.info(user.getName()+"角色權(quán)限為:"+authorities.toString());

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

四、編寫一個(gè)Controller用于測試權(quán)限

@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;
    }

}

后臺的內(nèi)容就實(shí)現(xiàn)了,接下來看前端如何限制角色訪問內(nèi)容

五、前端頁面限制角色訪問內(nèi)容

登錄頁核心內(nèi)容:

頁面需導(dǎo)入security和thymeleaf的整合標(biāo)簽
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>
主頁核心內(nèi)容:
<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}">武當(dāng)長拳</a></li>
        <li><a th:href="@{/level1/3}">全真劍法</a></li>
    </ul>
</div>

六、數(shù)據(jù)庫實(shí)現(xiàn)

為了測試,數(shù)據(jù)庫設(shè)計(jì)過于簡陋,但注意的問題為角色字段的內(nèi)容需要加上ROLE_前綴,否則會security會認(rèn)為此用戶依然沒有權(quán)限

image

密碼可以編寫一個(gè)工具類進(jìn)行加密
工具類如下:

@Component
public class BCryptUtil {

    private BCryptPasswordEncoder bCryptPasswordEncoder;

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

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

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

}

整合就到此完畢,時(shí)間較晚,如果有錯(cuò)漏盡管提出。

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容