您好,登錄后才能下訂單哦!
本文講述Spring Boot整合Spring Security在方法上使用注解實(shí)現(xiàn)權(quán)限控制,使用自定義UserDetailService,從MySQL中加載用戶信息。使用Security自帶的MD5加密,對(duì)用戶密碼進(jìn)行加密。頁(yè)面模板采用thymeleaf引擎。
源碼地址:https://github.com/li5454yong/springboot-security.git
1、引入pom依賴
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.4.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth3</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.15</version> </dependency> </dependencies>
這里使用druid連接池,Spring Data Jpa實(shí)現(xiàn)數(shù)據(jù)庫(kù)訪問。
2、配置Spring Security
@Configuration @EnableWebMvcSecurity @EnableGlobalMethodSecurity(prePostEnabled = true)//開啟security注解 public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Bean @Override protected AuthenticationManager authenticationManager() throws Exception { return super.authenticationManager(); } @Override protected void configure(HttpSecurity http) throws Exception { //允許所有用戶訪問"/"和"/home" http.authorizeRequests() .antMatchers("/", "/home").permitAll() //其他地址的訪問均需驗(yàn)證權(quán)限 .anyRequest().authenticated() .and() .formLogin() //指定登錄頁(yè)是"/login" .loginPage("/login") .defaultSuccessUrl("/hello")//登錄成功后默認(rèn)跳轉(zhuǎn)到"/hello" .permitAll() .and() .logout() .logoutSuccessUrl("/home")//退出登錄后的默認(rèn)url是"/home" .permitAll(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .userDetailsService(customUserDetailsService()) .passwordEncoder(passwordEncoder()); } /** * 設(shè)置用戶密碼的加密方式為MD5加密 * @return */ @Bean public Md5PasswordEncoder passwordEncoder() { return new Md5PasswordEncoder(); } /** * 自定義UserDetailsService,從數(shù)據(jù)庫(kù)中讀取用戶信息 * @return */ @Bean public CustomUserDetailsService customUserDetailsService(){ return new CustomUserDetailsService(); } }
這里只做了基本的配置,設(shè)置了登錄url、登錄成功后跳轉(zhuǎn)的url、退出后跳轉(zhuǎn)到的url。使用@EnableGlobalMethodSecurity(prePostEnabled = true)這個(gè)注解,可以開啟security的注解,我們可以在需要控制權(quán)限的方法上面使用@PreAuthorize,@PreFilter這些注解。
3、自定義userDetailService
public class CustomUserDetailsService implements UserDetailsService { @Autowired //數(shù)據(jù)庫(kù)服務(wù)類 private SUserService suserService; @Override public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException { //SUser對(duì)應(yīng)數(shù)據(jù)庫(kù)中的用戶表,是最終存儲(chǔ)用戶和密碼的表,可自定義 //本例使用SUser中的email作為用戶名: SUser user = suserService.findUserByEmail(userName); if (user == null) { throw new UsernameNotFoundException("UserName " + userName + " not found"); } // SecurityUser實(shí)現(xiàn)UserDetails并將SUser的Email映射為username SecurityUser securityUser = new SecurityUser(user); Collection<SimpleGrantedAuthority> authorities = new ArrayList<SimpleGrantedAuthority>(); authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN")); return securityUser; } }
這里只需要實(shí)現(xiàn)UserDetailsService 接口,重寫loadUserByUsername方法,從數(shù)據(jù)庫(kù)中取出用戶信息。最后返回一個(gè)UserDetails 實(shí)現(xiàn)類。
4、定義錯(cuò)誤處理配置
@Configuration public class ErrorPageConfig { @Bean public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){ return new MyCustomizer(); } private static class MyCustomizer implements EmbeddedServletContainerCustomizer { @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN, "/403")); } } }
訪問發(fā)生錯(cuò)誤時(shí),跳轉(zhuǎn)到”/403”.
5、Controller接口
@Controller public class IndexController { @Resource private SUserService sUserService; @RequestMapping("/home") public String home() { return "home"; } @PreAuthorize("hasRole('user')") @RequestMapping(value = "/admin",method = RequestMethod.GET) public String toAdmin(){ return "helloAdmin"; } @RequestMapping("/hello") public String hello() { return "hello"; } @RequestMapping("/login") public String login(){ return "login"; } @RequestMapping("/") public String root() { return "index"; } @RequestMapping("/403") public String error(){ return "403"; } }
在toAdmin()方法上面使用了@PreAuthorize(“hasRole(‘user')”),表示訪問這個(gè)方法需要擁有user角色。如果希望控制到權(quán)限層面,可以使用@PreAuthorize(“hasPermission()”)。這里只是用了其中的一個(gè)用法,更多的使用方法可以去看官方文檔。需要注意的是,Spring Security默認(rèn)的角色前綴是”ROLE_”,使用hasRole方法時(shí)已經(jīng)默認(rèn)加上了,因此我們?cè)跀?shù)據(jù)庫(kù)里面的用戶角色應(yīng)該是“ROLE_user”,在user前面加上”ROLE_”前綴。
6、測(cè)試
啟動(dòng)項(xiàng)目,訪問http://localhost:1130/login
點(diǎn)擊登錄后進(jìn)入到“/hello”
點(diǎn)擊跳轉(zhuǎn)到管理員頁(yè)面
在后臺(tái)的“/admin”這個(gè)url對(duì)應(yīng)的方法上面,限制了用戶必須要擁有“user”角色。在數(shù)據(jù)庫(kù)中也設(shè)置了登錄的用戶有這個(gè)角色。
現(xiàn)在我們修改數(shù)據(jù)庫(kù)中的用戶角色,改為“ROLE_admin”。退出登錄后重新登錄,再次點(diǎn)擊“前往管理員頁(yè)面”按鈕,會(huì)跳轉(zhuǎn)到如下頁(yè)面。
因?yàn)楝F(xiàn)在沒有了“user”權(quán)限,所以訪問的時(shí)候拋出了異常,被攔截后重定向到了“/403”。
7、POST方式訪問,錯(cuò)誤碼403
首先,把“/admin”改為POST請(qǐng)求
@PreAuthorize("hasRole('user')") @RequestMapping(value = "/admin",method = RequestMethod.POST) public String toAdmin(){ return "helloAdmin"; }
把“前往管理員頁(yè)面”按鈕的請(qǐng)求方式從原來的form表達(dá)get提交,改為ajax方式POST提交。至于為什么不是用form的POST提交后面再講。先修改代碼
<body> <h2 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h2> <!--<form th:action="@{/logout}" method="post"> <input type="submit" value="Sign Out"/> </form> <form th:action="@{/admin}" method="get"> <input th:type="submit" th:value="前往管理員頁(yè)面"/> </form>--> <a th:href="@{/admin}" rel="external nofollow" >前往管理員用戶頁(yè)面</a> <input th:type="submit" onclick="testPost()" th:value="前往管理員頁(yè)面"/> </body> <script> function testPost() { $.ajax({ url:"/admin", type:'POST', success:function (data) { } }); } </script>
點(diǎn)擊“前往管理員頁(yè)面”按鈕,在調(diào)試臺(tái)可以看到如下
這是因?yàn)榭蚣軆?nèi)部防止CSRF(Cross-site request forgery跨站請(qǐng)求偽造)的發(fā)生,限制了除了get以外的大多數(shù)方法。
下面說解決辦法:
首先在標(biāo)簽內(nèi)添加如下內(nèi)容。
<meta name="_csrf" th:content="${_csrf.token}"/> <meta name="_csrf_hader" th:content="${_csrf.headerName}"/>
只要添加這個(gè)token,后臺(tái)就會(huì)驗(yàn)證這個(gè)token的正確性,如果正確,則接受post訪問。
然后在ajax代碼中添加以下代碼:
var token = $('meta[name="_csrf"]').attr("content"); var header = $('meta[name="_csrf_hader"]').attr("content"); $(document).ajaxSend(function(e,xhr,opt){ xhr.setRequestHeader(header,token); });
這樣就可以正常使用POST、DELETE等其他方式來訪問了。
上面說到使用表單的POST方式來提交,通過查看頁(yè)面源代碼可以看到
框架在form表單中自動(dòng)插入了一個(gè)隱藏域,value值就是那個(gè)token,所以使用form表單來提交POST請(qǐng)求是可以直接通過的,而ajax方式提交的話,需要加上那段代碼。
好了,這篇文章就講到這,后面還會(huì)有文章講述REST API風(fēng)格如何來使用Spring Security來控制權(quán)限。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。