配置代码
最近笔者在学SpringSecurity和vue.js,
本篇文章仅是记录前后端分离方式的SpringSecurity登录的代码,
发在这里的原因是也许能帮助到小伙伴们。
vue.config.js //端口转发
let proxyObj = { } ;
const CompressionPlugin = require ( "compression-webpack-plugin" ) ;
proxyObj[ '/ws' ] = {
ws: true ,
target: "ws://localhost:8888"
} ;
proxyObj[ '/' ] = {
ws: false ,
target: 'http://localhost:8888' ,
changeOrigin: true ,
pathRewrite: {
'^/' : ''
}
}
module. exports = {
devServer: {
host: 'localhost' ,
port: 8080 ,
proxy: proxyObj
} ,
configureWebpack : config => {
if ( process. env. NODE_ENV === 'production' ) {
return {
plugins: [
new CompressionPlugin ( {
test: / \.js$|\.html$|\.css / ,
threshold: 1024 ,
deleteOriginalAssets: false
} )
]
}
}
}
}
MyPasswordEncoder.java
import org. springframework. security. crypto. password. PasswordEncoder ;
import org. springframework. stereotype. Component ;
import org. springframework. util. DigestUtils ;
@Component
public class MyPasswordEncoder implements PasswordEncoder {
@Override
public String encode ( CharSequence rawPassword) {
return DigestUtils . md5DigestAsHex ( rawPassword. toString ( ) . getBytes ( ) ) ;
}
@Override
public boolean matches ( CharSequence rawPassword, String encodedPassword) {
return encodedPassword. equals ( DigestUtils . md5DigestAsHex ( rawPassword. toString ( ) . getBytes ( ) ) ) ;
}
}
WebSecurityConfig.java
package com. huang. config ;
import com. huang. blog. service. UserService ;
import com. huang. blog. service. impl. UserServiceImpl ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
import org. springframework. security. config. annotation. authentication. builders. AuthenticationManagerBuilder ;
import org. springframework. security. config. annotation. web. builders. HttpSecurity ;
import org. springframework. security. config. annotation. web. builders. WebSecurity ;
import org. springframework. security. config. annotation. web. configuration. WebSecurityConfigurerAdapter ;
import org. springframework. security. web. access. AccessDeniedHandler ;
import java. io. PrintWriter ;
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
UserServiceImpl userService;
@Override
protected void configure ( AuthenticationManagerBuilder auth) throws Exception {
auth. userDetailsService ( userService) ;
}
@Override
protected void configure ( HttpSecurity http) throws Exception {
http. authorizeRequests ( )
. antMatchers ( "admin/**" ) . hasRole ( "管理员" )
. anyRequest ( ) . authenticated ( )
. and ( ) . formLogin ( )
. loginPage ( "/login_page" )
. loginProcessingUrl ( "/login" )
. usernameParameter ( "username" )
. passwordParameter ( "password" )
. permitAll ( )
. successHandler (
( httpServletRequest, httpServletResponse, authentication) -> {
httpServletResponse. setContentType ( "application/json;charset=utf-8" ) ;
PrintWriter out = httpServletResponse. getWriter ( ) ;
out. write ( "{\"status\":\"success\",\"msg\":\"登录成功\"}" ) ;
out. flush ( ) ;
out. close ( ) ;
}
)
. failureHandler ( ( httpServletRequest, httpServletResponse, e) -> {
httpServletResponse. setContentType ( "application/json;charset=utf-8" ) ;
PrintWriter out = httpServletResponse. getWriter ( ) ;
out. write ( "{\"status\":\"error\",\"msg\":\"登录失败\"}" ) ;
out. flush ( ) ;
out. close ( ) ;
} )
. and ( ) . logout ( ) . permitAll ( ) . and ( ) . csrf ( ) . disable ( ) . exceptionHandling ( )
. authenticationEntryPoint ( new AuthenticationExceptionHandler ( ) )
;
}
@Override
public void configure ( WebSecurity web) throws Exception {
web. ignoring ( ) . antMatchers ( "/blogimg/**" , "/index.html" , "/static/**" ) ;
}
}
AuthenticationExceptionHandler.java
package com. huang. config ;
import org. springframework. http. MediaType ;
import org. springframework. security. authentication. InsufficientAuthenticationException ;
import org. springframework. security. core. AuthenticationException ;
import org. springframework. security. web. AuthenticationEntryPoint ;
import org. springframework. stereotype. Component ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import java. io. IOException ;
@Component
public class AuthenticationExceptionHandler implements AuthenticationEntryPoint {
@Override
public void commence ( HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws
IOException {
response. setContentType ( "application/json;charset=utf-8" ) ;
response. getWriter ( ) . write ( "{\"status\":\"error\",\"msg\":\"登录失败\"}" ) ;
}
}
UserServiceImpl.java
package com. huang. blog. service. impl ;
import com. baomidou. mybatisplus. core. conditions. query. QueryWrapper ;
import com. huang. blog. mapper. RoleMapper ;
import com. huang. blog. pojo. Role ;
import com. huang. blog. pojo. User ;
import com. huang. blog. mapper. UserMapper ;
import com. huang. blog. service. UserService ;
import com. baomidou. mybatisplus. extension. service. impl. ServiceImpl ;
import lombok. extern. slf4j. Slf4j ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. security. core. userdetails. UserDetails ;
import org. springframework. security. core. userdetails. UserDetailsService ;
import org. springframework. security. core. userdetails. UsernameNotFoundException ;
import org. springframework. stereotype. Service ;
import java. sql. Wrapper ;
import java. util. HashMap ;
import java. util. HashSet ;
import java. util. List ;
import java. util. Map ;
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl < UserMapper , User > implements UserService , UserDetailsService {
@Autowired
public UserMapper userMapper;
@Autowired
public RoleMapper roleMapper;
@Override
public UserDetails loadUserByUsername ( String s) throws UsernameNotFoundException {
System . out. println ( s) ;
QueryWrapper < User > wrapper = new QueryWrapper < > ( ) ;
wrapper. eq ( "username" , s) ;
User user = userMapper. selectOne ( wrapper) ;
if ( user== null ) {
return new User ( ) ;
}
List < Role > list = roleMapper. selectRolesByUid ( user. getId ( ) ) ;
user. setRoles ( list) ;
return user;
}
}
RoleMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<! DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = " com.huang.blog.mapper.RoleMapper" >
< resultMap id = " BaseResultMap" type = " com.huang.blog.pojo.Role" >
< id column = " id" property = " id" />
< result column = " name" property = " name" />
</ resultMap>
< select id = " selectRolesByUid" parameterType = " long" resultMap = " BaseResultMap" >
select r.id as id,r.name as name from role r,role_user ru,user u where r.id=ru.rid and ru.uid=u.id and u.id =#{uid}
</ select>
< select id = " selectRolesById" resultType = " com.huang.blog.pojo.Role" >
select * from role where id=#{id}
</ select>
< sql id = " Base_Column_List" >
id
, name
</ sql>
</ mapper>
User.java
package com. huang. blog. pojo ;
import com. baomidou. mybatisplus. annotation. IdType ;
import java. util. ArrayList ;
import java. util. Collection ;
import java. util. Date ;
import com. baomidou. mybatisplus. annotation. TableField ;
import com. baomidou. mybatisplus. annotation. TableId ;
import java. io. Serializable ;
import java. util. List ;
import com. fasterxml. jackson. annotation. JsonIgnore ;
import io. swagger. annotations. ApiModel ;
import io. swagger. annotations. ApiModelProperty ;
import lombok. Data ;
import lombok. EqualsAndHashCode ;
import lombok. experimental. Accessors ;
import org. springframework. security. core. GrantedAuthority ;
import org. springframework. security. core. authority. SimpleGrantedAuthority ;
import org. springframework. security. core. userdetails. UserDetails ;
@Data
@EqualsAndHashCode ( callSuper = false )
@Accessors ( chain = true )
@ApiModel ( value = "User对象" , description = "" )
public class User implements Serializable , UserDetails {
private static final long serialVersionUID = 1L ;
@TableId ( value = "id" , type = IdType . AUTO)
private Long id;
private String username;
private String nickname;
private String password;
private Boolean enabled;
private String email;
private String userface;
@TableField ( exist = false )
private List < Role > roles;
@ApiModelProperty ( value = "注册时间" )
private Date regTime;
@ApiModelProperty ( value = "更新时间" )
private Date updTime;
@Override
@JsonIgnore
public boolean isAccountNonExpired ( ) {
return true ;
}
@Override
@JsonIgnore
public boolean isAccountNonLocked ( ) {
return true ;
}
@Override
@JsonIgnore
public boolean isCredentialsNonExpired ( ) {
return true ;
}
@Override
public boolean isEnabled ( ) {
return enabled;
}
@Override
@JsonIgnore
public List < GrantedAuthority > getAuthorities ( ) {
List < GrantedAuthority > authorities = new ArrayList < > ( ) ;
for ( Role role : roles) {
authorities. add ( new SimpleGrantedAuthority ( "ROLE_" + role. getName ( ) ) ) ;
}
return authorities;
}
}