Skip to content
Snippets Groups Projects
Commit ab1d877f authored by wyl's avatar wyl
Browse files

complete security frame

parent 4225b35b
No related branches found
No related tags found
1 merge request!28complete security frame
......@@ -22,7 +22,7 @@ public class AuthenticationSuccessHandler implements org.springframework.securit
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException{
String authorities = authentication.getAuthorities().toString();
System.out.println("authorities: " + authorities);
// Configuration response
response.setContentType("application/json;charset=UTF-8");
ObjectMapper objectMapper = new ObjectMapper();
......
......@@ -4,6 +4,7 @@ import com.cardiff.client_project.constant.ResponseCode;
import com.cardiff.client_project.pojo.dto.SelectDTO;
import com.cardiff.client_project.pojo.entity.CommonAdmin;
import com.cardiff.client_project.pojo.entity.Hospital;
import com.cardiff.client_project.pojo.entity.Patient;
import com.cardiff.client_project.pojo.entity.SuperUser;
import com.cardiff.client_project.pojo.vo.AdminVO;
......@@ -11,6 +12,7 @@ import com.cardiff.client_project.pojo.vo.HospitalVO;
import com.cardiff.client_project.pojo.vo.SelectVO;
import com.cardiff.client_project.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
......@@ -294,12 +296,33 @@ public class SuperAdminMapper {
* @param username
* @return
*/
public SuperUser getInforByName(String username){
String sql="select * from super_admin where name=?";
SuperUser superUser = jdbcTemplate.queryForObject(sql, new Object[]{username}, new BeanPropertyRowMapper<SuperUser>(SuperUser.class));
return superUser;
public Object getInforByName(String username){
try {
// 1. 查询 super_admin 表
String sql = "select * from super_admin where name=?";
return jdbcTemplate.queryForObject(sql, new Object[]{username}, new BeanPropertyRowMapper<>(SuperUser.class));
} catch (Exception s) {
try {
// 2. 查询 hospital 表
String sql = "select * from hospital where email=?";
return jdbcTemplate.queryForObject(sql, new Object[]{username}, new BeanPropertyRowMapper<>(Hospital.class));
} catch (Exception h) {
try {
// 3. 查询 common_admin 表
String sql = "select * from common_admin where email=?";
return jdbcTemplate.queryForObject(sql, new Object[]{username}, new BeanPropertyRowMapper<>(CommonAdmin.class));
} catch (Exception c) {
try {
// 4. 查询 patient 表
String sql = "select * from patient where email=?";
return jdbcTemplate.queryForObject(sql, new Object[]{username}, new BeanPropertyRowMapper<>(Patient.class));
} catch (Exception p) {}
}
}
}
// 如果所有查询都为空,返回 null
return null;
}
/**
......
......@@ -9,6 +9,7 @@ import lombok.ToString;
public class Hospital {
private int id;
private String name;
private String password;
private int amountPatient;
private int maxAmount;
private int patientId;
......@@ -16,6 +17,8 @@ public class Hospital {
private String type;
private String address;
private String phone;
private String email;
private int roleId;
private int status;
}
package com.cardiff.client_project.service;
import com.cardiff.client_project.mapper.SuperAdminMapper;
import com.cardiff.client_project.pojo.entity.CommonAdmin;
import com.cardiff.client_project.pojo.entity.Hospital;
import com.cardiff.client_project.pojo.entity.Patient;
import com.cardiff.client_project.pojo.entity.SuperUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
......@@ -18,14 +21,36 @@ public class UserLogin implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UserDetails userDetails = null;
System.out.println(username);
SuperUser inforByName = superAdminMapper.getInforByName(username);
System.out.println(inforByName);
String type = superAdminMapper.getTypeById(inforByName.getRoleId());
System.out.println(type);
SimpleGrantedAuthority authority = new SimpleGrantedAuthority(type);
System.out.println(authority);
UserDetails userDetails = User.withUsername(inforByName.getName()).password(inforByName.getPassword()).authorities(authority).build();
Object object = superAdminMapper.getInforByName(username);
if(object instanceof SuperUser){
SuperUser inforByName = (SuperUser) object;
String type = superAdminMapper.getTypeById(inforByName.getRoleId());
SimpleGrantedAuthority authority = new SimpleGrantedAuthority(type);
userDetails = User.withUsername(inforByName.getName()).password(inforByName.getPassword()).authorities(authority).build();
}
if(object instanceof CommonAdmin){
CommonAdmin inforByName = (CommonAdmin) object;
String type = superAdminMapper.getTypeById(inforByName.getRoleId());
SimpleGrantedAuthority authority = new SimpleGrantedAuthority(type);
userDetails = User.withUsername(inforByName.getName()).password(inforByName.getPassword()).authorities(authority).build();
}
if(object instanceof Hospital){
Hospital inforByName = (Hospital) object;
String type = superAdminMapper.getTypeById(inforByName.getRoleId());
SimpleGrantedAuthority authority = new SimpleGrantedAuthority(type);
userDetails = User.withUsername(inforByName.getName()).password(inforByName.getPassword()).authorities(authority).build();
}
if(object instanceof Patient){
Patient inforByName = (Patient) object;
String type = superAdminMapper.getTypeById(inforByName.getRoleId());
SimpleGrantedAuthority authority = new SimpleGrantedAuthority(type);
userDetails = User.withUsername(inforByName.getName()).password(inforByName.getPassword()).authorities(authority).build();
}
return userDetails;
}
......
......@@ -153,6 +153,15 @@
if (type == "[" + "SUPER" + "]") {
window.location.href = "superAdminView.html";
}
if (type == "[" + "ADMIN" + "]") {
window.location.href = "commonUser.html";
}
if (type == "[" + "PATIENT" + "]") {
window.location.href = "";
}
if (type == "[" + "HOSPITAL" + "]") {
window.location.href = "";
}
alert("login success");
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment