diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/com/zhidi/dao/LoginDao.java b/src/main/java/com/zhidi/dao/LoginDao.java new file mode 100644 index 0000000..afcdb5c --- /dev/null +++ b/src/main/java/com/zhidi/dao/LoginDao.java @@ -0,0 +1,29 @@ +package com.zhidi.dao; + +import com.zhidi.entity.User; +import com.zhidi.util.DBUitl; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +/** + * Created by Administrator on 2018/1/14/014. + */ +public class LoginDao { + + public User login(String username) throws SQLException { + PreparedStatement ps = DBUitl.getConnection().prepareStatement("select * from tb_users where user_name = ?"); + ps.setString(1, username); + ResultSet rs = ps.executeQuery(); + User user = null; + while (rs.next()) { + Integer id = rs.getInt("id"); + String userName = rs.getString("user_name"); + String password = rs.getString("password"); + String phone = rs.getString("phone"); + user = new User(id, userName, password, phone); + } + return user; + } +} diff --git a/src/main/java/com/zhidi/entity/User.java b/src/main/java/com/zhidi/entity/User.java new file mode 100644 index 0000000..1ff1f97 --- /dev/null +++ b/src/main/java/com/zhidi/entity/User.java @@ -0,0 +1,51 @@ +package com.zhidi.entity; + +/** + * Created by Administrator on 2018/1/14/014. + */ +public class User { + + private Integer id; + private String username; + private String password; + private String phone; + + public User(Integer id, String username, String password, String phone) { + this.id = id; + this.username = username; + this.password = password; + this.phone = phone; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/src/main/java/com/zhidi/filter/MyFilter1.java b/src/main/java/com/zhidi/filter/MyFilter1.java new file mode 100644 index 0000000..2a40640 --- /dev/null +++ b/src/main/java/com/zhidi/filter/MyFilter1.java @@ -0,0 +1,31 @@ +package com.zhidi.filter; + +import org.apache.log4j.Logger; + +import javax.servlet.*; +import javax.servlet.annotation.WebFilter; +import java.io.IOException; + +/** + * Created by Administrator on 2018/1/14/014. + */ +public class MyFilter1 implements Filter { + + private static final Logger log = Logger.getLogger(MyFilter1.class); + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + log.info("这是Filter1"); + filterChain.doFilter(servletRequest,servletResponse); + } + + @Override + public void destroy() { + + } +} diff --git a/src/main/java/com/zhidi/filter/MyFilter2.java b/src/main/java/com/zhidi/filter/MyFilter2.java new file mode 100644 index 0000000..79c99bd --- /dev/null +++ b/src/main/java/com/zhidi/filter/MyFilter2.java @@ -0,0 +1,30 @@ +package com.zhidi.filter; + +import org.apache.log4j.Logger; + +import javax.servlet.*; +import java.io.IOException; + +/** + * Created by Administrator on 2018/1/14/014. + */ +public class MyFilter2 implements Filter { + + private static final Logger log = Logger.getLogger(MyFilter2.class); + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + log.info("这是Filter2"); + filterChain.doFilter(servletRequest,servletResponse); + } + + @Override + public void destroy() { + + } +} diff --git a/src/main/java/com/zhidi/realm/LoginRealm.java b/src/main/java/com/zhidi/realm/LoginRealm.java new file mode 100644 index 0000000..395d958 --- /dev/null +++ b/src/main/java/com/zhidi/realm/LoginRealm.java @@ -0,0 +1,46 @@ +package com.zhidi.realm; + +import com.zhidi.dao.LoginDao; +import com.zhidi.entity.User; +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.authz.AuthorizationInfo; +import org.apache.shiro.authz.SimpleAuthorizationInfo; +import org.apache.shiro.realm.AuthorizingRealm; +import org.apache.shiro.subject.PrincipalCollection; + +import java.sql.SQLException; + +/** + * Created by Administrator on 2018/1/14/014. + */ +public class LoginRealm extends AuthorizingRealm { + + @Override + protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { + return null; + } + + @Override + protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { + + String username = String.valueOf(authenticationToken.getPrincipal()); + String password = String.valueOf((char[]) authenticationToken.getCredentials()); + User user = null; + try { + user = new LoginDao().login(username); + } catch (SQLException e) { + e.printStackTrace(); + } + if (user == null) { + return null; + } + if (!password.equals(user.getPassword())) { + return null; + } + AuthenticationInfo info = new SimpleAuthenticationInfo(username, password, this.getName()); + return info; + } +} diff --git a/src/main/java/com/zhidi/servlet/LoginServlet.java b/src/main/java/com/zhidi/servlet/LoginServlet.java new file mode 100644 index 0000000..ca1717e --- /dev/null +++ b/src/main/java/com/zhidi/servlet/LoginServlet.java @@ -0,0 +1,63 @@ +package com.zhidi.servlet; + +import org.apache.log4j.Logger; +import org.apache.shiro.SecurityUtils; +import org.apache.shiro.authc.*; +import org.apache.shiro.mgt.SecurityManager; +import org.apache.shiro.subject.Subject; +import org.apache.shiro.web.env.WebEnvironment; +import org.apache.shiro.web.util.WebUtils; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * Created by Administrator on 2018/1/14/014. + */ +@WebServlet("/login") +public class LoginServlet extends HttpServlet { + + private static final transient Logger log = Logger.getLogger(LoginServlet.class); + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + doPost(req, resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String username = req.getParameter("username"); + String password = req.getParameter("password"); + Boolean rememberMe = Boolean.valueOf(req.getParameter("rememberMe")); + + WebEnvironment webEnvironment = WebUtils.getRequiredWebEnvironment(req.getServletContext()); + SecurityManager securityManager = webEnvironment.getSecurityManager(); + SecurityUtils.setSecurityManager(securityManager); + Subject currentUser = SecurityUtils.getSubject(); + if (!currentUser.isAuthenticated()) { + UsernamePasswordToken token = new UsernamePasswordToken(username, password); + token.setRememberMe(rememberMe); + try { + currentUser.login(token); + } catch (UnknownAccountException un) { + log.info("用户不存在"); + return; + }catch ( IncorrectCredentialsException ice ) { + //password didn't match, try again? + log.info("密码错误"); + } catch ( LockedAccountException lae ) { + //account for that username is locked - can't login. Show them a message? + log.info("账户被锁定,无法登录"); + } catch ( AuthenticationException ae ) { + //unexpected condition - error? + log.info("未知错误..."); + } + + } + resp.sendRedirect(req.getContextPath() + "/index.jsp"); + } +} diff --git a/src/main/java/com/zhidi/util/DBUitl.java b/src/main/java/com/zhidi/util/DBUitl.java new file mode 100644 index 0000000..65f67ba --- /dev/null +++ b/src/main/java/com/zhidi/util/DBUitl.java @@ -0,0 +1,31 @@ +package com.zhidi.util; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +/** + * Created by Administrator on 2018/1/14/014. + */ +public class DBUitl { + + private static final String username = "root"; + private static final String password = "root"; + private static final String url = "jdbc:mysql://localhost:3306/db_hibernate"; + + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + + public static Connection getConnection() throws SQLException { + Connection conn = DriverManager.getConnection(url, username, password); + return conn; + } + + +} diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties new file mode 100644 index 0000000..ca7b485 --- /dev/null +++ b/src/main/resources/log4j.properties @@ -0,0 +1,19 @@ +### direct log messages to stdout ### +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.Target=System.out +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n + +### direct messages to file hibernate.log ### +#log4j.appender.file=org.apache.log4j.FileAppender +#log4j.appender.file.File=hibernate.log +#log4j.appender.file.layout=org.apache.log4j.PatternLayout +#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n + +### set log levels - for more verbose logging change 'info' to 'debug' ### + +log4j.rootLogger=warn, stdout,info + +#log4j.logger.org.hibernate=info +log4j.logger.org.apache.shiro=debug +log4j.logger.com.zhidi=debug diff --git a/src/main/webapp/WEB-INF/shiro.ini b/src/main/webapp/WEB-INF/shiro.ini new file mode 100644 index 0000000..36189ab --- /dev/null +++ b/src/main/webapp/WEB-INF/shiro.ini @@ -0,0 +1,24 @@ + +[main] +#自定义过滤器 +myFilter1 = com.zhidi.filter.MyFilter1 +myFilter2 = com.zhidi.filter.MyFilter2 + +loginRealm = com.zhidi.realm.LoginRealm +securityManager.realm = $loginRealm + +#默认过滤器 +authc.loginUrl = /login.jsp + +[users] +mjj = 123,sysadmin,Guest + + +[roles] +sysadmin = * + + +[urls] +#/index.jsp = myFilter1, myFilter2 +/login.jsp = authc +/user/* = authc \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..7a72dbf --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,52 @@ + + + + + + + + + org.apache.shiro.web.env.EnvironmentLoaderListener + + + + + ShiroFilter + org.apache.shiro.web.servlet.ShiroFilter + + + + ShiroFilter + /* + REQUEST + FORWARD + INCLUDE + ERROR + + + + + + \ No newline at end of file diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp new file mode 100644 index 0000000..b1cdb0c --- /dev/null +++ b/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

Hello World!sss

+ + diff --git a/src/main/webapp/login.jsp b/src/main/webapp/login.jsp new file mode 100644 index 0000000..99d4e28 --- /dev/null +++ b/src/main/webapp/login.jsp @@ -0,0 +1,26 @@ +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2018/1/14/014 + Time: 19:09 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<% + String path = request.getContextPath(); + String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; +%> + + + + Title + + +
+
+
+ 记住我
+ +
+ + diff --git a/src/main/webapp/user/index.jsp b/src/main/webapp/user/index.jsp new file mode 100644 index 0000000..1c164f4 --- /dev/null +++ b/src/main/webapp/user/index.jsp @@ -0,0 +1,16 @@ +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2018/1/14/014 + Time: 18:27 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 主页 + + +HHHH + +