SpringBoot +Spring Security + th
2020-05-28 本文已影响0人
YL君
SpringBoot +Spring Security + thymeleaf + layui简单整合微信公众号
程序思路以及资源来源:
这个是用于本人毕业设计中绑定微信公众号推送消息的目的写的。该项目功能有:【模版消息推送】,【自动回复】,【微信用户详情】这3个功能,但是可以作为一个demo提供给大家,第一次弄公众号方面有问题请多多提出。
首先给看看具体样子:
wx-login.pngwx-index.png
Maven依赖
<properties>
<weixin-java-mp.version>3.6.0</weixin-java-mp.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<swagger.version>2.7.0</swagger.version>
<mysql-connector.version>5.1.46</mysql-connector.version>
<druid.version>1.1.12</druid.version>
<mp-starter.version>3.1.0</mp-starter.version>
<mybatis-starter.version>2.0.1</mybatis-starter.version>
<pagehelper.version>5.1.2</pagehelper.version>
<pagehelper-auto.version>1.2.3</pagehelper-auto.version>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.locales>zh_CN</project.build.locales>
<docker.image.prefix>wechat-mp-demo</docker.image.prefix>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>${weixin-java-mp.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!-- Testing Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.46</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.version}</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
<version>${pagehelper-auto.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-starter.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mp-starter.version}</version>
</dependency>
<dependency>
<groupId>com.bscommon</groupId>
<artifactId>bscommon-base</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
调通WX平台接口配置
注意:认证时候写别导入Security ,因为不配做Security 的话会拦截认证请求
先登录微信平台,可以看到appid和appsecret,接口配置信息去可以设置token和配置文件一样,url为认证路径
yml配置如图下(aesKey可以不用,正式公众号才需要)
wx-yml.png
然后具体接口直接引用上方wx-demo的配置和认证接口即可,(该项目就是在该demo上进行修改的),
接口写好后运行程序,然后在微信平台上填写url和token,url为[ip]/wx/portal/[appid]
,[]为序替换的,
注意该认证端口为80或者为443,在本地测试的话可以用natApp进行内网穿透,有免费的通道可以用。
配置好点击体检,上方提示成功就Ok了。
配置Security
应为毕业原因认证方面需要自定义加密(MD5),而Security默认的BCrypt几码。还有为了解决layui 中当session过期post请求不跳转login页面,需要自定义成功/失败处理器,具体如下 如下:
/**
* 配置文件
**/
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private static final String KEY = "yljun.com";
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private LoginValidateAuthenticationProvider loginValidateAuthenticationProvider;
@Autowired
private CustomAuthenticationFailure customAuthenticationFailure; //自定义认证失败处理
@Autowired
private CustomAuthenticationSuccess customAuthenticationSuccess; //自定义认证认证处理
@Bean
public PasswordEncoder passwordEncoder() {
// return new BCryptPasswordEncoder(); // 使用 BCrypt 加密
return new CustomPasswordEncoder(); //自定义加密
}
/**
* 自定义配置 wx/** 不拦截微信平台认证
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("wx/**","error/**", "lib/**","js/**", "api/**", "images/**", "css/**").permitAll() // 都可以访问
.antMatchers("/h2-console/**").permitAll() // 都可以访问
.antMatchers("/wxm/**").hasRole("1") // 需要相应的角色才能访问
.antMatchers("/index", "/", "/welcome").hasRole("1")
.and()
.formLogin().failureHandler(customAuthenticationFailure).successHandler(customAuthenticationSuccess) //基于 Form 表单登录验证
.loginPage("/login") // 自定义登录界面
.and().rememberMe().key(KEY); // 启用 remember me
// .and().exceptionHandling().accessDeniedPage("/403"); // 处理异常,拒绝访问就重定向到 403 页面
http.csrf().disable(); //关闭跨域保护
http.headers().frameOptions().sameOrigin(); // 允许来自同一来源的H2 控制台的请求
}
/**
* 认证信息管理
* @param auth
* @throws Exception
*/
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
auth.authenticationProvider(loginValidateAuthenticationProvider);
}
}
具体功能
完成了微信公众号消息推送以及关注用户分页获取,还有简单的公众号自动回复功能。