diff --git a/examples/quickstart/src/main/java/org/springside/examples/quickstart/entity/Task.java b/examples/quickstart/src/main/java/org/springside/examples/quickstart/entity/Task.java index dfe8d030..baeb0a99 100644 --- a/examples/quickstart/src/main/java/org/springside/examples/quickstart/entity/Task.java +++ b/examples/quickstart/src/main/java/org/springside/examples/quickstart/entity/Task.java @@ -1,10 +1,12 @@ package org.springside.examples.quickstart.entity; import javax.persistence.Entity; +import javax.persistence.Table; import org.hibernate.validator.constraints.NotBlank; @Entity +@Table(name = "SS_TASK") public class Task extends IdEntity { private String title; @@ -18,5 +20,4 @@ public class Task extends IdEntity { public void setTitle(String title) { this.title = title; } - } diff --git a/examples/quickstart/src/main/java/org/springside/examples/quickstart/entity/User.java b/examples/quickstart/src/main/java/org/springside/examples/quickstart/entity/User.java new file mode 100644 index 00000000..70701e8c --- /dev/null +++ b/examples/quickstart/src/main/java/org/springside/examples/quickstart/entity/User.java @@ -0,0 +1,66 @@ +package org.springside.examples.quickstart.entity; + +import javax.persistence.Entity; +import javax.persistence.Table; +import javax.persistence.Transient; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.hibernate.validator.constraints.NotBlank; + +@Entity +@Table(name = "SS_USER") +public class User extends IdEntity { + private String loginName; + private String name; + private String plainPassword; + private String password; + private String salt; + + @NotBlank + public String getLoginName() { + return loginName; + } + + public void setLoginName(String loginName) { + this.loginName = loginName; + } + + @NotBlank + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Transient + public String getPlainPassword() { + return plainPassword; + } + + public void setPlainPassword(String plainPassword) { + this.plainPassword = plainPassword; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getSalt() { + return salt; + } + + public void setSalt(String salt) { + this.salt = salt; + } + + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this); + } +} \ No newline at end of file diff --git a/examples/quickstart/src/main/java/org/springside/examples/quickstart/repository/UserDao.java b/examples/quickstart/src/main/java/org/springside/examples/quickstart/repository/UserDao.java new file mode 100644 index 00000000..02f690a8 --- /dev/null +++ b/examples/quickstart/src/main/java/org/springside/examples/quickstart/repository/UserDao.java @@ -0,0 +1,8 @@ +package org.springside.examples.quickstart.repository; + +import org.springframework.data.repository.PagingAndSortingRepository; +import org.springside.examples.quickstart.entity.User; + +public interface UserDao extends PagingAndSortingRepository { + User findByLoginName(String loginName); +} diff --git a/examples/quickstart/src/main/java/org/springside/examples/quickstart/rest/TaskRestController.java b/examples/quickstart/src/main/java/org/springside/examples/quickstart/rest/TaskRestController.java index 3199153f..251455d4 100644 --- a/examples/quickstart/src/main/java/org/springside/examples/quickstart/rest/TaskRestController.java +++ b/examples/quickstart/src/main/java/org/springside/examples/quickstart/rest/TaskRestController.java @@ -28,7 +28,6 @@ import org.springside.modules.beanvalidator.BeanValidators; * Task的Restful API的Controller. * * @author calvin - * */ @Controller @RequestMapping(value = "/api/v1/task") diff --git a/examples/quickstart/src/main/java/org/springside/examples/quickstart/service/AccountService.java b/examples/quickstart/src/main/java/org/springside/examples/quickstart/service/AccountService.java new file mode 100644 index 00000000..f42392fa --- /dev/null +++ b/examples/quickstart/src/main/java/org/springside/examples/quickstart/service/AccountService.java @@ -0,0 +1,53 @@ +package org.springside.examples.quickstart.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; +import org.springside.examples.quickstart.entity.User; +import org.springside.examples.quickstart.repository.UserDao; +import org.springside.modules.security.utils.Digests; +import org.springside.modules.utils.Encodes; + +/** + * 用户管理类. + * + * @author calvin + */ +//Spring Service Bean的标识. +@Component +@Transactional(readOnly = true) +public class AccountService { + + public static final String HASH_ALGORITHM = "SHA-1"; + public static final int HASH_INTERATIONS = 1024; + private static final int SALT_SIZE = 8; + + private UserDao userDao; + + private ShiroDbRealm shiroRealm; + + public User findUserByLoginName(String loginName) { + return userDao.findByLoginName(loginName); + } + + public void registerUser(User user) { + entryptPassword(user); + userDao.save(user); + } + + /** + * 设定安全的密码,生成随机的salt并经过1024次 sha-1 hash + */ + private void entryptPassword(User user) { + byte[] salt = Digests.generateSalt(SALT_SIZE); + user.setSalt(Encodes.encodeHex(salt)); + + byte[] hashPassword = Digests.sha1(user.getPlainPassword().getBytes(), salt, HASH_INTERATIONS); + user.setPassword(Encodes.encodeHex(hashPassword)); + } + + @Autowired + public void setUserDao(UserDao userDao) { + this.userDao = userDao; + } +} diff --git a/examples/quickstart/src/main/java/org/springside/examples/quickstart/service/ShiroDbRealm.java b/examples/quickstart/src/main/java/org/springside/examples/quickstart/service/ShiroDbRealm.java new file mode 100644 index 00000000..b5cf7f97 --- /dev/null +++ b/examples/quickstart/src/main/java/org/springside/examples/quickstart/service/ShiroDbRealm.java @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.springside.examples.quickstart.service; + +import java.io.Serializable; + +import javax.annotation.PostConstruct; + +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; +import org.apache.shiro.authc.AuthenticationException; +import org.apache.shiro.authc.AuthenticationInfo; +import org.apache.shiro.authc.AuthenticationToken; +import org.apache.shiro.authc.SimpleAuthenticationInfo; +import org.apache.shiro.authc.UsernamePasswordToken; +import org.apache.shiro.authc.credential.HashedCredentialsMatcher; +import org.apache.shiro.authz.AuthorizationInfo; +import org.apache.shiro.realm.AuthorizingRealm; +import org.apache.shiro.subject.PrincipalCollection; +import org.apache.shiro.util.ByteSource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springside.examples.quickstart.entity.User; +import org.springside.modules.utils.Encodes; + +public class ShiroDbRealm extends AuthorizingRealm { + + protected AccountService accountService; + + /** + * 认证回调函数,登录时调用. + */ + @Override + protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException { + UsernamePasswordToken token = (UsernamePasswordToken) authcToken; + User user = accountService.findUserByLoginName(token.getUsername()); + if (user != null) { + byte[] salt = Encodes.decodeHex(user.getSalt()); + return new SimpleAuthenticationInfo(new ShiroUser(user.getLoginName(), user.getName()), user.getPassword(), + ByteSource.Util.bytes(salt), getName()); + } else { + return null; + } + } + + /** + * 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用. + */ + @Override + protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { + return null; + } + + /** + * 设定Password校验的Hash算法与迭代次数. + */ + @PostConstruct + public void initCredentialsMatcher() { + HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(AccountService.HASH_ALGORITHM); + matcher.setHashIterations(AccountService.HASH_INTERATIONS); + + setCredentialsMatcher(matcher); + } + + @Autowired + public void setAccountService(AccountService accountService) { + this.accountService = accountService; + } + + /** + * 自定义Authentication对象,使得Subject除了携带用户的登录名外还可以携带更多信息. + */ + public static class ShiroUser implements Serializable { + private static final long serialVersionUID = -1373760761780840081L; + public String loginName; + public String name; + + public ShiroUser(String loginName, String name) { + this.loginName = loginName; + this.name = name; + } + + public String getName() { + return name; + } + + /** + * 本函数输出将作为默认的输出. + */ + @Override + public String toString() { + return loginName; + } + + /** + * 重载equals,只计算loginName; + */ + @Override + public int hashCode() { + return HashCodeBuilder.reflectionHashCode(this, "loginName"); + } + + /** + * 重载equals,只比较loginName + */ + @Override + public boolean equals(Object obj) { + return EqualsBuilder.reflectionEquals(this, obj, "loginName"); + } + } +} diff --git a/examples/quickstart/src/main/java/org/springside/examples/quickstart/web/LoginController.java b/examples/quickstart/src/main/java/org/springside/examples/quickstart/web/LoginController.java new file mode 100644 index 00000000..94609844 --- /dev/null +++ b/examples/quickstart/src/main/java/org/springside/examples/quickstart/web/LoginController.java @@ -0,0 +1,31 @@ +package org.springside.examples.quickstart.web; + +import org.apache.shiro.web.filter.authc.FormAuthenticationFilter; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * LoginController负责打开登录页面(GET请求)和登录出错页面(POST请求), + + * 真正登录的POST请求由Filter完成, + * + * @author calvin + */ +@Controller +public class LoginController { + + @RequestMapping(value = "/login", method = RequestMethod.GET) + public String login() { + return "login"; + } + + @RequestMapping(value = "/login", method = RequestMethod.POST) + public String fail(@RequestParam(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM) String userName, Model model) { + model.addAttribute(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM, userName); + return "login"; + } + +} diff --git a/examples/quickstart/src/main/resources/applicationContext-shiro.xml b/examples/quickstart/src/main/resources/applicationContext-shiro.xml new file mode 100644 index 00000000..1886c427 --- /dev/null +++ b/examples/quickstart/src/main/resources/applicationContext-shiro.xml @@ -0,0 +1,40 @@ + + + + Shiro安全配置 + + + + + + + + + + + + + + + + + + + + /login = authc + /logout = logout + /task* = user + + + + + + + + + + + + \ No newline at end of file diff --git a/examples/quickstart/src/main/resources/ehcache/ehcache-shiro.xml b/examples/quickstart/src/main/resources/ehcache/ehcache-shiro.xml new file mode 100644 index 00000000..48b5c7a7 --- /dev/null +++ b/examples/quickstart/src/main/resources/ehcache/ehcache-shiro.xml @@ -0,0 +1,12 @@ + + + + \ No newline at end of file diff --git a/examples/quickstart/src/main/resources/sql/h2/schema.sql b/examples/quickstart/src/main/resources/sql/h2/schema.sql index 2753d654..c7414208 100644 --- a/examples/quickstart/src/main/resources/sql/h2/schema.sql +++ b/examples/quickstart/src/main/resources/sql/h2/schema.sql @@ -1,8 +1,18 @@ -drop table if exists task; +drop table if exists ss_task; +drop table if exists ss_user; -create table task ( +create table ss_task ( id bigint generated by default as identity, title varchar(255) not null, primary key (id) -) ; \ No newline at end of file +); + +create table ss_user ( + id bigint generated by default as identity, + login_name varchar(64) not null unique, + name varchar(64), + password varchar(255), + salt varchar(64), + primary key (id) +); \ No newline at end of file diff --git a/examples/quickstart/src/main/webapp/WEB-INF/layouts/header.jsp b/examples/quickstart/src/main/webapp/WEB-INF/layouts/header.jsp index a9fec05a..2a977767 100644 --- a/examples/quickstart/src/main/webapp/WEB-INF/layouts/header.jsp +++ b/examples/quickstart/src/main/webapp/WEB-INF/layouts/header.jsp @@ -1,8 +1,22 @@ <%@ page language="java" pageEncoding="UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %> \ No newline at end of file diff --git a/examples/quickstart/src/main/webapp/WEB-INF/views/login.jsp b/examples/quickstart/src/main/webapp/WEB-INF/views/login.jsp new file mode 100644 index 00000000..edd86895 --- /dev/null +++ b/examples/quickstart/src/main/webapp/WEB-INF/views/login.jsp @@ -0,0 +1,58 @@ +<%@ 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" %> + + + + 登录页 + + + + + + <% + String error = (String) request.getAttribute(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME); + if(error != null){ + %> +
+
+
+ 登录失败,请重试. +
+
+
+ <% + } + %> + +
+ +
+ +
+
+
+ +
+ +
+
+ +
+
+ + +

(管理员admin/admin, 普通用户user/user)

+
+
+
+ + diff --git a/examples/quickstart/src/main/webapp/WEB-INF/web.xml b/examples/quickstart/src/main/webapp/WEB-INF/web.xml index 2bfad46e..f4870181 100644 --- a/examples/quickstart/src/main/webapp/WEB-INF/web.xml +++ b/examples/quickstart/src/main/webapp/WEB-INF/web.xml @@ -1,77 +1,101 @@ - - QuickStart - - contextConfigLocation - - classpath*:/applicationContext.xml + + QuickStart + + + contextConfigLocation + + classpath*:/applicationContext.xml, + classpath*:/applicationContext-shiro.xml - - - spring.profiles.default - production - - - org.springframework.web.context.ContextLoaderListener - - - encodingFilter - org.springframework.web.filter.CharacterEncodingFilter - - encoding - UTF-8 - - - forceEncoding - true - - - - encodingFilter - /* - - - openEntityManagerInViewFilter - org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter - - - openEntityManagerInViewFilter - /* - - - sitemeshFilter - com.opensymphony.sitemesh.webapp.SiteMeshFilter - - - sitemeshFilter - /* - - - springServlet - org.springframework.web.servlet.DispatcherServlet - - contextConfigLocation - /WEB-INF/spring-mvc.xml - - 1 - - - springServlet - / - - - 20 - - - java.lang.Throwable - /WEB-INF/views/error/500.jsp - - - 500 - /WEB-INF/views/error/500.jsp - - - 404 - /WEB-INF/views/error/404.jsp - + + + spring.profiles.default + production + + + + org.springframework.web.context.ContextLoaderListener + + + + encodingFilter + org.springframework.web.filter.CharacterEncodingFilter + + encoding + UTF-8 + + + forceEncoding + true + + + + encodingFilter + /* + + + + openEntityManagerInViewFilter + org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter + + + openEntityManagerInViewFilter + /* + + + + shiroFilter + org.springframework.web.filter.DelegatingFilterProxy + + targetFilterLifecycle + true + + + + shiroFilter + /* + + + + sitemeshFilter + com.opensymphony.sitemesh.webapp.SiteMeshFilter + + + sitemeshFilter + /* + + + + springServlet + org.springframework.web.servlet.DispatcherServlet + + contextConfigLocation + /WEB-INF/spring-mvc.xml + + 1 + + + springServlet + / + + + + 20 + + + + java.lang.Throwable + /WEB-INF/views/error/500.jsp + + + 500 + /WEB-INF/views/error/500.jsp + + + 404 + /WEB-INF/views/error/404.jsp + \ No newline at end of file diff --git a/examples/quickstart/src/test/functional/org/springside/examples/quickstart/functional/QuickStartServer.java b/examples/quickstart/src/test/functional/org/springside/examples/quickstart/functional/QuickStartServer.java index 29763486..a2fe97f1 100644 --- a/examples/quickstart/src/test/functional/org/springside/examples/quickstart/functional/QuickStartServer.java +++ b/examples/quickstart/src/test/functional/org/springside/examples/quickstart/functional/QuickStartServer.java @@ -20,7 +20,7 @@ public class QuickStartServer { //启动Jetty Server server = JettyFactory.createServerInSource(PORT, CONTEXT); - JettyFactory.setTldJarNames(server, "sitemesh", "spring-webmvc"); + JettyFactory.setTldJarNames(server, "sitemesh", "spring-webmvc", "shiro-web"); try { server.start(); diff --git a/examples/quickstart/src/test/resources/data/sample-data.xml b/examples/quickstart/src/test/resources/data/sample-data.xml index ce4af4da..7de8bbe7 100644 --- a/examples/quickstart/src/test/resources/data/sample-data.xml +++ b/examples/quickstart/src/test/resources/data/sample-data.xml @@ -1,8 +1,10 @@ - - - - - + + + + + + +