提交 0e7670e7 编写于 作者: 黄勇

【I】将 Smart Security 模块转换为 Smart Security 插件,并提供基于注解的授权特性

上级 7b09202e
......@@ -65,7 +65,7 @@ INSERT INTO `product_type` VALUES ('2', 'Tablet Computer', '平板电脑');
-- ----------------------------
-- Table structure for log
-- ----------------------------
DROP TABLE IF EXISTS `log`;
CREATE TABLE `log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`date` char(10) DEFAULT NULL,
......@@ -73,3 +73,45 @@ CREATE TABLE `log` (
`description` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for role
-- ----------------------------
DROP TABLE IF EXISTS `role`;
CREATE TABLE `role` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`role_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for permission
-- ----------------------------
DROP TABLE IF EXISTS `permission`;
CREATE TABLE `permission` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`permission_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for user_role
-- ----------------------------
DROP TABLE IF EXISTS `user_role`;
CREATE TABLE `user_role` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) DEFAULT NULL,
`role_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for role_permission
-- ----------------------------
DROP TABLE IF EXISTS `role_permission`;
CREATE TABLE `role_permission` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`role_id` bigint(20) DEFAULT NULL,
`permission_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
......@@ -56,11 +56,6 @@
<groupId>org.smart4j</groupId>
<artifactId>smart-framework</artifactId>
<version>${smart.version}</version>
</dependency>
<dependency>
<groupId>org.smart4j</groupId>
<artifactId>smart-security</artifactId>
<version>${smart.version}</version>
</dependency>
<dependency>
<groupId>org.smart4j</groupId>
......@@ -92,6 +87,11 @@
<artifactId>smart-plugin-druid</artifactId>
<version>${smart.version}</version>
</dependency>
<dependency>
<groupId>org.smart4j</groupId>
<artifactId>smart-plugin-security</artifactId>
<version>${smart.version}</version>
</dependency>
</dependencies>
<build>
......
......@@ -4,7 +4,7 @@ import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.smart4j.framework.dao.DatabaseHelper;
import org.smart4j.security.SmartSecurity;
import org.smart4j.plugin.security.SmartSecurity;
public class CustomSmartSecurity implements SmartSecurity {
......@@ -22,7 +22,7 @@ public class CustomSmartSecurity implements SmartSecurity {
}
@Override
public Set<String> getPermNameSet(String roleName) {
public Set<String> getPermissionNameSet(String roleName) {
String sql = "select distinct p.permission_name from role r, role_permission rp, permission p where r.id = rp.role_id and p.id = rp.permission_id and r.role_name = ?";
List<String> list = DatabaseHelper.queryColumnList(sql, roleName);
return new LinkedHashSet<String>(list);
......
......@@ -13,6 +13,7 @@ import org.smart4j.framework.mvc.bean.Params;
import org.smart4j.framework.mvc.bean.Result;
import org.smart4j.framework.mvc.bean.View;
import org.smart4j.framework.util.WebUtil;
import org.smart4j.plugin.security.annotation.HasPermissions;
import org.smart4j.sample.Constant;
import org.smart4j.sample.Tool;
import org.smart4j.sample.bean.ProductBean;
......@@ -48,6 +49,7 @@ public class ProductAction {
.data("productBeanPager", productBeanPager);
}
@HasPermissions("product.create")
@Request.Get("/product/create")
public View create() {
List<ProductType> productTypeList = productService.getProductTypeList();
......
......@@ -8,9 +8,9 @@ import org.smart4j.framework.mvc.bean.Result;
import org.smart4j.framework.mvc.bean.View;
import org.smart4j.framework.util.StringUtil;
import org.smart4j.framework.util.WebUtil;
import org.smart4j.plugin.security.SecurityHelper;
import org.smart4j.plugin.security.fault.LoginException;
import org.smart4j.sample.Constant;
import org.smart4j.security.SmartSecurityHelper;
import org.smart4j.security.exception.LoginException;
@Action
public class SystemAction {
......@@ -38,7 +38,7 @@ public class SystemAction {
}
try {
SmartSecurityHelper.login(username, password, false);
SecurityHelper.login(username, password, false);
} catch (LoginException e) {
return new Result(false);
}
......@@ -48,7 +48,7 @@ public class SystemAction {
@Request.Get("/logout")
public Result logout() {
SmartSecurityHelper.logout();
SecurityHelper.logout();
return new Result(true);
}
}
package org.smart4j.sample.service.impl;
import org.smart4j.framework.tx.annotation.Service;
import org.smart4j.plugin.security.SecurityHelper;
import org.smart4j.sample.service.UserService;
import org.smart4j.security.SmartSecurityHelper;
@Service
public class UserServiceImpl implements UserService {
@Override
public void login(String username, String password) {
SmartSecurityHelper.login(username, password, false);
SecurityHelper.login(username, password, false);
}
}
common.title=Smart Sample
common.copyright=Copyright \u00A9 2013
common.user=User
common.logout=Logout
common.logout_confirm=Do you want to logout system?
common.action=Action
......@@ -23,6 +24,7 @@ common.captcha.change=Change a new Captcha
common.captcha.error=Captcha error!
error.page=Error Page
error.page.403=Access to the specified resource has been forbidden!
error.page.404=The requested resource is not available!
error.page.500=Internal server error!
......
common.title=Smart \u793A\u4F8B
common.copyright=\u7248\u6743\u6240\u6709 \u00A9 2013
common.user=\u7528\u6237
common.logout=\u6CE8\u9500
common.logout_confirm=\u4F60\u60F3\u6CE8\u9500\u7CFB\u7EDF\u5417\uFF1F
common.action=\u64CD\u4F5C
......@@ -23,6 +24,7 @@ common.captcha.change=\u6362\u4E00\u4E2A\u65B0\u7684\u9A8C\u8BC1\u7801
common.captcha.error=\u9A8C\u8BC1\u7801\u9519\u8BEF\uFF01
error.page=\u9519\u8BEF\u9875\u9762
error.page.403=\u7981\u6B62\u8BBF\u95EE\u6307\u5B9A\u7684\u8D44\u6E90\uFF01
error.page.404=\u8BF7\u6C42\u7684\u8D44\u6E90\u4E0D\u53EF\u7528\uFF01
error.page.500=\u5185\u90E8\u670D\u52A1\u5668\u9519\u8BEF\uFF01
......
[main]
authc.loginUrl = /login
authc.loginUrl=/login
[urls]
/ = anon
/product/** = authc
\ No newline at end of file
/=anon
/product/**=authc
\ No newline at end of file
......@@ -16,4 +16,4 @@ smart.security.realms=custom
smart.security.custom.class=org.smart4j.sample.CustomSmartSecurity
smart.security.cache=true
sample.upload_path=/www/upload/
sample.upload_path=/www/upload/
\ No newline at end of file
<%@ page pageEncoding="UTF-8" %>
<%@ taglib prefix="security" uri="/security" %>
<div id="header">
<div id="logo">
<div class="logo">
<a href="${BASE}/"><f:message key="common.title"/></a>
</div>
<div id="menu">
<div class="menu">
<a href="${BASE}/product"><f:message key="product"/></a>
</div>
<div id="oper">
<div class="oper">
<span><f:message key="common.user"/>: <security:principal/></span>
<button type="button" id="logout"><f:message key="common.logout"/></button>
</div>
</div>
\ No newline at end of file
<%@ page pageEncoding="UTF-8" %>
<%@ include file="../common/global.jsp" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="../common/meta.jsp" %>
<title><f:message key="common.title"/> - <f:message key="error.page"/></title>
<%@ include file="../common/style.jsp" %>
</head>
<body>
<div id="header">
<div class="logo">
<a href="${BASE}/"><f:message key="common.title"/></a>
</div>
</div>
<div id="content">
<div id="main">
<h3>403 - <f:message key="error.page.403"/></h3>
<a href="javascript:history.back();"><f:message key="common.back"/></a>
</div>
</div>
<%@ include file="../common/footer.jsp" %>
<%@ include file="../common/script.jsp" %>
</body>
</html>
\ No newline at end of file
......@@ -11,7 +11,7 @@
<body>
<div id="header">
<div id="logo">
<div class="logo">
<a href="${BASE}/"><f:message key="common.title"/></a>
</div>
</div>
......
......@@ -11,7 +11,7 @@
<body>
<div id="header">
<div id="logo">
<div class="logo">
<a href="${BASE}/"><f:message key="common.title"/></a>
</div>
</div>
......
......@@ -11,7 +11,7 @@
<body>
<div id="header">
<div id="logo">
<div class="logo">
<a href="${BASE}/"><f:message key="common.title"/></a>
</div>
</div>
......
<%@ page pageEncoding="UTF-8" %>
<%@ include file="common/global.jsp" %>
<%@ taglib prefix="security" uri="/security" %>
<!DOCTYPE html>
<html>
......@@ -31,7 +32,9 @@
</form>
</div>
<div class="css-right">
<a href="${BASE}/product/create"><f:message key="product.new_product"/></a>
<security:hasPermission name="product.create">
<a href="${BASE}/product/create"><f:message key="product.new_product"/></a>
</security:hasPermission>
</div>
</div>
<div id="product_list">
......
......@@ -5,6 +5,10 @@
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<error-page>
<error-code>403</error-code>
<location>/WEB-INF/jsp/error/403.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/jsp/error/404.jsp</location>
......
......@@ -113,24 +113,28 @@ img {
#header a {
color: #FFF;
}
#header #logo {
#header .logo {
float: left;
font-size: 16px;
font-weight: bold;
margin: 10px;
}
#header #menu {
#header .menu {
float: left;
margin: 10px;
}
#header #menu a {
#header .menu a {
margin: 10px;
}
#header #oper {
#header .oper {
float: right;
margin: 10px;
}
#header #oper button {
#header .oper span {
height: 20px;
padding: 0 5px;
}
#header .oper button {
height: 20px;
padding: 0 5px;
}
......
package org.smart4j.sample.test;
import org.apache.shiro.authc.credential.DefaultPasswordService;
import org.apache.shiro.authc.credential.PasswordService;
import org.junit.Assert;
import org.junit.Test;
public class ShiroTest {
@Test
public void passwordTest() {
String plaintextPassword = "admin";
PasswordService passwordService = new DefaultPasswordService();
String encryptPassword = passwordService.encryptPassword(plaintextPassword);
System.out.println("encryptPassword: " + encryptPassword);
Assert.assertTrue(passwordService.passwordsMatch(plaintextPassword, encryptPassword));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册