+ * Sets the default prefix to be added to {@link #hasAnyRole(String...)} or + * {@link #hasRole(String)}. For example, if hasRole("ADMIN") or hasRole("ROLE_ADMIN") + * is passed in, then the role ROLE_ADMIN will be used when the defaultRolePrefix is + * "ROLE_" (default). + *
+ * + *+ * If null or empty, then no default role prefix is used. + *
+ * + * @param defaultRolePrefix the default prefix to add to roles. Default "ROLE_". + */ + public void setDefaultRolePrefix(String defaultRolePrefix) { + this.defaultRolePrefix = defaultRolePrefix; + } + + private Set$session.getAttribute('username')
+ Online +
+ #foreach($e in $stackTrace)
+ $e
+ #end
+
+
+
+#parse("/common/footer.html")
+
+```
+
+
+
+
+##6.测试运行
+
+为了方便测试用户权限功能,我们给数据库初始化一些测试数据进去:
+
+```
+package com.springboot.in.action.service
+
+import java.util.UUID
+import javax.annotation.PostConstruct
+
+import com.springboot.in.action.dao.{RoleDao, UserDao, UserRoleDao}
+import com.springboot.in.action.entity.{Role, User, UserRole}
+import org.springframework.beans.factory.annotation.Autowired
+import org.springframework.stereotype.Service
+
+/**
+ * Created by jack on 2017/4/29.
+ * 初始化测试数据
+ */
+@Service // 需要初始化数据时,打开注释即可。
+class DataInit @Autowired()(val userDao: UserDao,
+ val userRoleDao: UserRoleDao,
+ val roleDao: RoleDao) {
+
+ @PostConstruct def dataInit(): Unit = {
+ val uuid = UUID.randomUUID().toString
+
+ val admin = new User
+ val jack = new User
+
+ admin.username = "admin_" + uuid
+ admin.password = "admin"
+
+ jack.username = "jack_" + uuid
+ jack.password = "123456"
+
+ userDao.save(admin)
+ userDao.save(jack)
+
+ val adminRole = new Role
+ val userRole = new Role
+
+ adminRole.role = "ROLE_ADMIN"
+ userRole.role = "ROLE_USER"
+
+ roleDao.save(adminRole)
+ roleDao.save(userRole)
+
+ val userRoleAdminRecord1 = new UserRole
+ userRoleAdminRecord1.userId = admin.id
+ userRoleAdminRecord1.roleId = adminRole.id
+ userRoleDao.save(userRoleAdminRecord1)
+
+ val userRoleAdminRecord2 = new UserRole
+ userRoleAdminRecord2.userId = admin.id
+ userRoleAdminRecord2.roleId = userRole.id
+ userRoleDao.save(userRoleAdminRecord2)
+
+ val userRoleJackRecord = new UserRole
+ userRoleJackRecord.userId = jack.id
+ userRoleJackRecord.roleId = userRole.id
+ userRoleDao.save(userRoleJackRecord)
+
+
+ }
+
+}
+
+```
+
+
+同样的,在我们需要权限控制的页面对应的方法上添加@PreAuthorize注解,value="hasRole('ADMIN')")或"hasRole('USER')"等。
+
+部署应用,访问http://localhost:8888/httpapi , 我们可以看到系统自动拦截跳转到登录页面
+
+
+
+
+
+输入USER角色的用户名jack,密码123456,系统跳转到默认登录成功页面。我们访问无权限页面http://localhost:8888/httpreport ,可以看出,系统拦截到无权限,跳转到了错误提示页面
+
+
+
+
+
+###HttpSecurity, WebSecurity和AuthenticationManagerBuilder
+
+
+
+
+
+小结
+===
+本文通过一个最简单的示例完成了对Web应用的安全控制,Spring Security提供的功能还远不止于此,更多Spring Security的使用可参见【参考资料】部分。
+
+
+
+
+
+
+
+
+
+
+参考资料:
+0.http://baike.baidu.com/item/spring%20security
+1.http://elim.iteye.com/blog/2247073
+2.http://blog.csdn.net/u012373815/article/details/54632176
+3.https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-secure
+4.http://www.open-open.com/lib/view/open1464482054012.html
+5.https://github.com/EasySpringBoot/spring-security
+6.http://docs.spring.io/spring-security/site/docs/4.1.0.RELEASE/reference/htmlsingle/#jc-authentication
+7.https://github.com/pzxwhc/MineKnowContainer/issues/58
+8.http://stackoverflow.com/questions/22998731/httpsecurity-websecurity-and-authenticationmanagerbuilder
+9.https://spring.io/blog/2013/07/03/spring-security-java-config-preview-web-security/
+10.https://springcloud.cc/spring-security-zhcn.html
+
+
+
+
+
diff --git a/pom.xml b/pom.xml
index a241184..948eb76 100644
--- a/pom.xml
+++ b/pom.xml
@@ -7,23 +7,21 @@
-+如果您对我们感兴趣,请联系我们
-