概述
定义业务层的实现类:MemberLoginService
package com.gwolf.service;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
import com.gwolf.vo.Member;
public class MemberLoginService {
private Connection connection;
private static final String DBDRIVER="org.gjt.mm.mysql.Driver";
private static final String DBURL="jdbc:mysql://localhost:3306/shirodb";
private static final String DBUSER = "root";
private static final String PASSWORD="root";
private PreparedStatement pstmt;
public MemberLoginService() {
this.connectionDataBase();
}
public Member get(String mid) {
Member vo = null;
try {
String sql = "select mid,password from member where mid=?";
this.pstmt = this.connection.prepareStatement(sql);
this.pstmt.setString(1, mid);
ResultSet rs = this.pstmt.executeQuery();
if(rs.next()) {
vo = new Member();
vo.setMid(rs.getString(1));
vo.setPassword(rs.getString(2));
}
} catch (SQLException e) {
e.printStackTrace();
}
return vo;
}
/**
* 根据用户名查询出永不对应的所有的角色数据
* @param mid
* @return
*/
public Set listRolesByMember(String mid) {
Set allRoles = new HashSet<>();
try {
String sql = "select flag from role where rid in"
+ "(select rid from member_role where mid=?)";
this.pstmt = this.connection.prepareStatement(sql);
this.pstmt.setString(1, mid);
ResultSet rs = this.pstmt.executeQuery();
while(rs.next()) {
allRoles.add(rs.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
return allRoles;
}
/**
* 根据用户名查询出所有的权限数据
* @param mid
* @return
*/
public Set listActionByMember(String mid) {
Set allAction = new HashSet<>();
try {
String sql = "select flag from action where actid in"
+ "(select actid from role_action where rid"
+ " in(select rid from member_role where mid=?))";
this.pstmt = this.connection.prepareStatement(sql);
this.pstmt.setString(1, mid);
ResultSet rs = this.pstmt.executeQuery();
while(rs.next()) {
allAction.add(rs.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
return allAction;
}
public void close() {
if(this.connection!=null) {
try {
this.connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private void connectionDataBase() {
try {
Class.forName(DBDRIVER);
connection = DriverManager.getConnection(DBURL, DBUSER, PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
}
}
最后
以上就是顺利帅哥为你收集整理的shiro利用mysql动态授权_如何实现Shiro基于数据库的授权认证的全部内容,希望文章能够帮你解决shiro利用mysql动态授权_如何实现Shiro基于数据库的授权认证所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复