引入依赖
spring security web mybatis druid连接池 mysql
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.28</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
application.properties
spring.datasource.username=**
spring.datasource.password=**
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/vhr
server.port=8081
主配置类扫描xml文件和接口
@MapperScan(basePackages = "org.lyb.baiweiserver.mapper ") //扫描配置文件
Service查询用户
@Service
public class HrService implements UserDetailsService {
@Autowired
HrMapper hrMapper;
@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
Hr hr = hrMapper.loadUserByUsername(s);
if (hr == null){
throw new UsernameNotFoundException("用户不存在");
}
return hr;
}
}
Mapper
public interface HrMapper {
/**
* 根据用户名登陆
* @param username 用户名
* @return
*/
Hr loadUserByUsername(String username);
HrMapper.xml
<select id="loadUserByUsername" resultMap="BaseResultMap">
select * from hr where username=#{username};
</select>
config里面
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
HrService hrService;
/**
* 加密密码编码器
* @return
*/
@Bean
PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests() //授权
.anyRequest().authenticated() //任何请求要认证
.and()
.formLogin() //登录
.loginProcessingUrl("/doLogin")//登陆要处理的请求
.successHandler(new AuthenticationSuccessHandler() { //身份验证成功处理程序
/**
* 认证成功方法
* @param req Http Servlet请求
* @param resp 响应
* @param authentication 认证
* @throws IOException
* @throws ServletException
*/
@Override
public void onAuthenticationSuccess(HttpServletRequest req, HttpServletResponse resp,
Authentication authentication) throws IOException, ServletException {
resp.setContentType("application/json;charset=utf-8"); //响应字体
Hr hr =(Hr) authentication.getPrincipal(); //获取对象认证
RespBean respBean = RespBean.ok("登录成功", hr); //自定义封装响应请求信息
ObjectMapper objectMapper = new ObjectMapper(); //转换器
String s = objectMapper.writeValueAsString(respBean); //改成字符串
PrintWriter out = resp.getWriter(); //获取流
out.write(s); //写入
out.flush();
}
})
.failureHandler(new AuthenticationFailureHandler() {
/**
* 身份验证失败处
* @param req 请求
* @param resp 响应
* @param exception 验证异常
* @throws IOException
* @throws ServletException
*/
@Override
public void onAuthenticationFailure(HttpServletRequest req,
HttpServletResponse resp, AuthenticationException exception) throws IOException, ServletException {
resp.setContentType("application/json;charset=utf-8");
RespBean respBean = RespBean.error("登陆失败"); //自定义封装异常
if (exception instanceof BadCredentialsException){
respBean.setMsg("用户名或密码错误");
}else if (exception instanceof DisabledException){
respBean.setMsg("账户被禁用,请联系管理员");
}
ObjectMapper objectMapper = new ObjectMapper();//转换器
String s = objectMapper.writeValueAsString(respBean); //改字符串
PrintWriter writer = resp.getWriter();
writer.write(s); //失败信息写入流中
writer.flush();
writer.close();
}
})
.permitAll() //其它请求
.and()
.csrf()
.disable();
}
}
model
/**
*封装请求信息
*/
public class RespBean {
private Integer status; //状态码
private String msg; //描叙
private Object obj; //可以带对象
public static RespBean ok(String msg, Object obj) {
return new RespBean(200, msg, obj);
}
public static RespBean ok(String msg) {
return new RespBean(200, msg, null);
}
public static RespBean error(String msg, Object obj) {
return new RespBean(500, msg, obj);
}
public static RespBean error(String msg) {
return new RespBean(500, msg, null);
}
private RespBean() {
}
private RespBean(Integer status, String msg, Object obj) {
this.status = status;
this.msg = msg;
this.obj = obj;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
}
http://localhost:8081/doLogin接下来和前端对接