#57 shiro权限校验移到showcase
This commit is contained in:
@@ -50,7 +50,7 @@
|
||||
<div class="controls">
|
||||
<label class="checkbox inline" for="rememberMe"> <input type="checkbox" id="rememberMe" name="rememberMe"/> 记住我</label>
|
||||
<input id="submit" class="btn" type="submit" value="登录"/>
|
||||
<p class="help-block">(管理员<b>admin/admin</b>, 普通用户<b>user/user</b>)</p>
|
||||
<p class="help-block">(登录帐号:<b>user/user</b>)</p>
|
||||
</div>
|
||||
</div>
|
||||
</form:form>
|
||||
|
||||
-1
@@ -64,5 +64,4 @@ public class TaskGuiFT extends BaseSeleniumTestCase {
|
||||
|
||||
assertEquals("必选字段", s.getText(By.xpath("//fieldset/div/div/label")));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-2
@@ -57,8 +57,7 @@ public class TaskRestFT extends BaseFunctionalTestCase {
|
||||
@Test
|
||||
@Category(Smoke.class)
|
||||
public void getTask() {
|
||||
Long id = 1L;
|
||||
Task task = restTemplate.getForObject(resoureUrl + "/{id}", Task.class, id);
|
||||
Task task = restTemplate.getForObject(resoureUrl + "/{id}", Task.class, 1L);
|
||||
assertEquals("Study PlayFramework 2.0", task.getTitle());
|
||||
}
|
||||
|
||||
|
||||
+3
-6
@@ -4,9 +4,6 @@ import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Sort;
|
||||
@@ -21,13 +18,13 @@ public class TasdkDaoTest extends SpringTransactionalTestCase {
|
||||
@Autowired
|
||||
private TaskDao taskDao;
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Test
|
||||
public void findTasksByUserId() throws Exception {
|
||||
List<Task> tasks = taskDao.findByUserId(1L, new Sort(Direction.ASC, "id"));
|
||||
assertEquals(5, tasks.size());
|
||||
assertEquals(new Long(1), tasks.get(0).getId());
|
||||
|
||||
tasks = taskDao.findByUserId(99999L, new Sort(Direction.ASC, "id"));
|
||||
assertEquals(0, tasks.size());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
package org.springside.examples.showcase.entity;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Transient;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.hibernate.annotations.Cache;
|
||||
import org.hibernate.annotations.CacheConcurrencyStrategy;
|
||||
@@ -20,6 +25,8 @@ public class Role extends IdEntity {
|
||||
|
||||
private String name;
|
||||
|
||||
private String permissions;
|
||||
|
||||
@Column(nullable = false, unique = true)
|
||||
public String getName() {
|
||||
return name;
|
||||
@@ -29,6 +36,19 @@ public class Role extends IdEntity {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getPermissions() {
|
||||
return permissions;
|
||||
}
|
||||
|
||||
public void setPermissions(String permissions) {
|
||||
this.permissions = permissions;
|
||||
}
|
||||
|
||||
@Transient
|
||||
public List<String> getPermissionList() {
|
||||
return Arrays.asList(StringUtils.split(permissions, ","));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ToStringBuilder.reflectionToString(this);
|
||||
|
||||
+2
@@ -77,6 +77,8 @@ public class ShiroDbRealm extends AuthorizingRealm {
|
||||
for (Role role : user.getRoleList()) {
|
||||
//基于Role的权限信息
|
||||
info.addRole(role.getName());
|
||||
//基于Permission的权限信息
|
||||
info.addStringPermissions(role.getPermissionList());
|
||||
}
|
||||
return info;
|
||||
} else {
|
||||
|
||||
+9
-3
@@ -4,6 +4,9 @@ import java.util.List;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.apache.shiro.authz.annotation.Logical;
|
||||
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||||
import org.apache.shiro.authz.annotation.RequiresRoles;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
@@ -27,6 +30,8 @@ public class UserController {
|
||||
@Autowired
|
||||
private AccountService accountService;
|
||||
|
||||
//特别设定多个ReuiresRole之间为Or关系,而不是默认的And.
|
||||
@RequiresRoles(value = { "Admin", "User" }, logical = Logical.OR)
|
||||
@RequestMapping(value = { "list", "" })
|
||||
public String list(Model model) {
|
||||
List<User> users = accountService.getAllUser();
|
||||
@@ -34,20 +39,21 @@ public class UserController {
|
||||
return "account/userList";
|
||||
}
|
||||
|
||||
@RequiresRoles("Admin")
|
||||
@RequestMapping(value = "update/{id}")
|
||||
public String updateForm(@PathVariable("id") Long id, Model model) {
|
||||
model.addAttribute("user", accountService.getUser(id));
|
||||
|
||||
List<String> allStatus = Lists.newArrayList("enabled", "disabled");
|
||||
model.addAttribute("allStatus", allStatus);
|
||||
return "account/userForm";
|
||||
}
|
||||
|
||||
@RequiresPermissions("user:edit")
|
||||
@RequestMapping(value = "save/{id}")
|
||||
public String update(@Valid @ModelAttribute("user") User user, RedirectAttributes redirectAttributes) {
|
||||
accountService.saveUser(user);
|
||||
redirectAttributes.addFlashAttribute("message", "保存用户成功");
|
||||
return "redirect:/account/user/";
|
||||
return "redirect:/account/user";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "checkLoginName")
|
||||
@@ -68,7 +74,7 @@ public class UserController {
|
||||
* 因为仅update()方法的form中有id属性,因此本方法在该方法中执行.
|
||||
*/
|
||||
@ModelAttribute("user")
|
||||
private User getUser(@RequestParam(value = "id", required = false) Long id) {
|
||||
public User getUser(@RequestParam(value = "id", required = false) Long id) {
|
||||
if (id != null) {
|
||||
return accountService.getUser(id);
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
<value>
|
||||
/login = authc
|
||||
/logout = logout
|
||||
/account/user/update/* = user
|
||||
/account/** = user
|
||||
/rest/users = authcBasic
|
||||
</value>
|
||||
</property>
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
create table ss_role (
|
||||
id bigint generated by default as identity,
|
||||
name varchar(255) not null unique,
|
||||
permissions varchar(255),
|
||||
primary key (id)
|
||||
);
|
||||
|
||||
|
||||
@@ -2,8 +2,11 @@
|
||||
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
|
||||
<div id="header" class="span12 page-header">
|
||||
<h1>Showcase示例<small>--开源项目大派对</small></h1>
|
||||
<shiro:guest>
|
||||
<span class="pull-right"><a href="${ctx}/login">登录</a></span>
|
||||
</shiro:guest>
|
||||
<shiro:user>
|
||||
<span class="pull-right">你好, <shiro:principal property="name"/> </span>
|
||||
<span class="pull-right">你好, <shiro:principal property="name"/> <a href="${ctx}/logout">退出登录</a></span>
|
||||
</shiro:user>
|
||||
|
||||
</div>
|
||||
@@ -7,12 +7,6 @@
|
||||
<div class="submenu">
|
||||
<ul>
|
||||
<li id="account-tab"><a href="${ctx}/account/user/">帐号管理</a></li>
|
||||
<shiro:guest>
|
||||
<li><a href="${ctx}/login">登录</a></li>
|
||||
</shiro:guest>
|
||||
<shiro:user>
|
||||
<li><a href="${ctx}/logout">退出登录</a></li>
|
||||
</shiro:user>
|
||||
</ul>
|
||||
</div>
|
||||
<h1>独立演示</h1>
|
||||
|
||||
@@ -25,5 +25,23 @@
|
||||
|
||||
<!-- 定义无Controller的path<->view直接映射 -->
|
||||
<mvc:view-controller path="/" view-name="index"/>
|
||||
<mvc:view-controller path="/web/mashup-client" view-name="/web/mashup-client"/>
|
||||
<mvc:view-controller path="/web/mashup-client" view-name="/web/mashup-client"/>
|
||||
|
||||
<!-- 支持 Shiro对Controller的方法级AOP安全控制 begin-->
|
||||
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
|
||||
<property name="proxyTargetClass" value="true" />
|
||||
</bean>
|
||||
|
||||
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
|
||||
<property name="securityManager" ref="securityManager"/>
|
||||
</bean>
|
||||
|
||||
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
|
||||
<property name="exceptionMappings">
|
||||
<props>
|
||||
<prop key="org.apache.shiro.authz.UnauthorizedException">error/403</prop>
|
||||
</props>
|
||||
</property>
|
||||
</bean>
|
||||
<!-- end -->
|
||||
</beans>
|
||||
|
||||
@@ -40,14 +40,14 @@
|
||||
<div class="control-group">
|
||||
<label for="loginName" class="control-label">登录名:</label>
|
||||
<div class="controls">
|
||||
<input type="text" id="loginName" name="loginName" size="50" value="${user.loginName}"/>
|
||||
<input type="text" id="loginName" name="loginName" size="50" value="${user.loginName}" class="required"/>
|
||||
<form:errors path="loginName" cssClass="error"/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="control-group">
|
||||
<label for="name" class="control-label">用户名:</label>
|
||||
<div class="controls">
|
||||
<input type="text" id="name" name="name" size="50" value="${user.name}"/>
|
||||
<input type="text" id="name" name="name" size="50" value="${user.name}" class="required"/>
|
||||
<form:errors path="name" cssClass="error"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<%@ page contentType="text/html;charset=UTF-8" %>
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
|
||||
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
|
||||
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
|
||||
<html>
|
||||
<head>
|
||||
@@ -34,7 +35,11 @@
|
||||
<td>${user.email} </td>
|
||||
<td>${user.roleNames} </td>
|
||||
<td>${user.status} </td>
|
||||
<td><a href="${ctx}/account/user/update/${user.id}" id="editLink-${user.loginName}">修改</a></td>
|
||||
<td>
|
||||
<shiro:hasPermission name="user:edit">
|
||||
<a href="${ctx}/account/user/update/${user.id}" id="editLink-${user.loginName}">修改</a>
|
||||
</shiro:hasPermission>
|
||||
</td>
|
||||
</tr>
|
||||
</c:forEach>
|
||||
</tbody>
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
<%@ page contentType="text/html;charset=UTF-8" %>
|
||||
<%@ page import="org.apache.shiro.web.filter.authc.FormAuthenticationFilter"%>
|
||||
<%@ page import="org.apache.shiro.authc.ExcessiveAttemptsException"%>
|
||||
<%@ page import="org.apache.shiro.authc.IncorrectCredentialsException"%>
|
||||
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
|
||||
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
|
||||
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
|
||||
<html>
|
||||
<head>
|
||||
<title>403 - 用户权限不足</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div><h1>用户权限不足.</h1></div>
|
||||
<div><a href="<c:url value="/"/>">返回首页</a></div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,18 @@
|
||||
<%@ page contentType="text/html;charset=UTF-8"%>
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
|
||||
|
||||
<%response.setStatus(200);%>
|
||||
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>404 - 页面不存在</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div>
|
||||
<div><h1>页面不存在.</h1></div>
|
||||
<div><a href="<c:url value="/"/>">返回首页</a></div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -7,7 +7,7 @@
|
||||
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
|
||||
<html>
|
||||
<head>
|
||||
<title>Showcase 登录页面</title>
|
||||
<title>登录页</title>
|
||||
<script src="${ctx}/static/jquery-validation/1.9.0/jquery.validate.min.js" type="text/javascript"></script>
|
||||
<script src="${ctx}/static/jquery-validation/1.9.0/messages_cn.js" type="text/javascript"></script>
|
||||
<link href="${ctx}/static/jquery-validation/1.9.0/validate.css" type="text/css" rel="stylesheet" />
|
||||
|
||||
+1
-1
@@ -57,7 +57,7 @@ public class BaseFunctionalTestCase {
|
||||
JettyFactory.setTldJarNames(jettyServer, ShowcaseServer.TLD_JAR_NAMES);
|
||||
jettyServer.start();
|
||||
|
||||
logger.info("Jetty Server started");
|
||||
logger.info("Jetty Server started at " + baseUrl);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+14
-6
@@ -20,19 +20,18 @@ public class UserManagerFT extends BaseSeleniumTestCase {
|
||||
public void list() {
|
||||
s.open("/");
|
||||
s.click(By.linkText("帐号管理"));
|
||||
loginAsAdminIfNecessary();
|
||||
assertEquals("Showcase示例:综合演示用例", s.getTitle());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Category(Smoke.class)
|
||||
public void editUser() {
|
||||
s.open("/");
|
||||
s.click(By.linkText("综合演示"));
|
||||
s.click(By.id("editLink-user"));
|
||||
s.click(By.linkText("帐号管理"));
|
||||
loginAsAdminIfNecessary();
|
||||
|
||||
//修改用户需要登录管理员权限
|
||||
s.type(By.name("username"), "admin");
|
||||
s.type(By.name("password"), "admin");
|
||||
s.click(By.id("submit"));
|
||||
s.click(By.id("editLink-user"));
|
||||
|
||||
//点击提交按钮
|
||||
s.type(By.name("name"), "user_foo");
|
||||
@@ -49,4 +48,13 @@ public class UserManagerFT extends BaseSeleniumTestCase {
|
||||
s.getSelect(By.name("status")).selectByValue("enabled");
|
||||
s.click(By.id("submit"));
|
||||
}
|
||||
|
||||
private void loginAsAdminIfNecessary() {
|
||||
//修改用户需要登录管理员权限
|
||||
if ("Showcase示例:登录页".equals(s.getTitle())) {
|
||||
s.type(By.name("username"), "admin");
|
||||
s.type(By.name("password"), "admin");
|
||||
s.click(By.id("submit"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
<SS_USER ID="4" EMAIL="kate@springside.org.cn" LOGIN_NAME="user3" NAME="Kate" PASSWORD="2488aa0c31c624687bd9928e0a5d29e7d1ed520b" SALT="6d65d24122c30500" STATUS="enabled" TEAM_ID="1"/>
|
||||
<SS_USER ID="5" EMAIL="sawyer@springside.org.cn" LOGIN_NAME="user4" NAME="Sawyer" PASSWORD="2488aa0c31c624687bd9928e0a5d29e7d1ed520b" SALT="6d65d24122c30500" STATUS="enabled" TEAM_ID="1"/>
|
||||
<SS_USER ID="6" EMAIL="ben@springside.org.cn" LOGIN_NAME="user5" NAME="Ben" PASSWORD="2488aa0c31c624687bd9928e0a5d29e7d1ed520b" SALT="6d65d24122c30500" STATUS="enabled" TEAM_ID="1"/>
|
||||
<SS_ROLE ID="1" NAME="Admin"/>
|
||||
<SS_ROLE ID="2" NAME="User"/>
|
||||
<SS_ROLE ID="1" NAME="Admin" PERMISSIONS="user:view,user:edit"/>
|
||||
<SS_ROLE ID="2" NAME="User" PERMISSIONS="user:view"/>
|
||||
<SS_USER_ROLE USER_ID="1" ROLE_ID="1"/>
|
||||
<SS_USER_ROLE USER_ID="1" ROLE_ID="2"/>
|
||||
<SS_USER_ROLE USER_ID="2" ROLE_ID="2"/>
|
||||
|
||||
Reference in New Issue
Block a user