Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/main/java/com/giit/www/system/controller/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.security.auth.Subject;
Expand All @@ -26,24 +28,37 @@ public class LoginController {
UserBiz userBiz;

@RequestMapping("login")
public String login(HttpServletRequest req, Model model, HttpSession session) {
String exceptionClassName = (String) req.getAttribute("shiroLoginFailure");
public String login(@RequestParam("username") String username, @RequestParam("password") String password) {

/*String exceptionClassName = (String) req.getAttribute("shiroLoginFailure");
System.out.println(exceptionClassName);
String error = null;
if (UnknownAccountException.class.getName().equals(exceptionClassName)) {
error = "用户名/密码错误";
} else if (IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
error = "用户名/密码错误";
} else if (exceptionClassName != null) {
error = "其他错误:" + exceptionClassName;
}
}*/

//TODO 这里以后可以把角色更换成资源控制后动态生成页面,(-->这里有疑问-->是不是可以使用自定义角色?shiro张开涛的16章有个自定义标签扫描出的角色)
org.apache.shiro.subject.Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
try {
subject.login(token);
} catch(IncorrectCredentialsException e) {
e.printStackTrace();
return "redirect:login.jsp";
} catch(Exception e) {
e.printStackTrace();
return "redirect:login.jsp";
}

boolean isAuthenticated = subject.isAuthenticated();

if (isAuthenticated) {
String principal = (String) subject.getPrincipal();
session.setAttribute("username", principal);
//session.setAttribute("username", principal);

switch (principal) {
case "admin":
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/giit/www/system/controller/MainController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.giit.www.system.controller;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

Expand All @@ -26,4 +28,14 @@ public String studentMainView() {
public String teacherMainView() {
return "/teacher/main";
}

public static void main(String[] args) {
String hashAlgorithmName = "MD5";
Object credential = "123456";
Object salt = ByteSource.Util.bytes("user");


Object result = new SimpleHash(hashAlgorithmName,credential,salt,1);
System.out.println(result);
}
}
4 changes: 2 additions & 2 deletions src/main/resources/db.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/giit
jdbc.url=jdbc:mysql://localhost:3306/giit?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=0313
jdbc.password=
4 changes: 2 additions & 2 deletions src/main/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Global logging configuration
#在开发环境下日志级别要设置成DEBUG 生产环境设置成info或error

log4j.rootLogger=DEBUG, stdout
log4j.rootLogger=INFO, stdout
#Console output...

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
4 changes: 4 additions & 0 deletions src/main/resources/mybatis-config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@

<configuration>

<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>

</configuration>
4 changes: 2 additions & 2 deletions target/classes/db.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/giit
jdbc.url=jdbc:mysql://localhost:3306/giit?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=0313
jdbc.password=
4 changes: 2 additions & 2 deletions target/giit/WEB-INF/classes/db.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/giit
jdbc.url=jdbc:mysql://localhost:3306/giit?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=0313
jdbc.password=