浏览器访问:https://start.spring.io 然后进入idea>>File>>open打开工程
1.在pom.xml文件中添加依赖 2.配置application.yml文件 3.配置WebConfiguration
@Configuration public class WebConfiguration implements WebMvcConfigurer { /** * druidServlet注册 */ @Bean public ServletRegistrationBean druidServletRegistration() { ServletRegistrationBean registration = new ServletRegistrationBean(new StatViewServlet()); registration.addUrlMappings("/druid/*"); return registration; } /** * druid监控 配置URI拦截策略 */ @Bean public FilterRegistrationBean druidStatFilter() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); //添加过滤规则. filterRegistrationBean.addUrlPatterns("/*"); //添加不需要忽略的格式信息. filterRegistrationBean.addInitParameter( "exclusions", "/static/*,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid,/druid/*"); //用于session监控页面的用户名显示 需要登录后主动将username注入到session里 filterRegistrationBean.addInitParameter("principalSessionName", "username"); return filterRegistrationBean; } /** * druid数据库连接池监控 */ @Bean public DruidStatInterceptor druidStatInterceptor() { return new DruidStatInterceptor(); } @Bean public JdkRegexpMethodPointcut druidStatPointcut() { JdkRegexpMethodPointcut druidStatPointcut = new JdkRegexpMethodPointcut(); String patterns = "com.example.demo.service.*"; //可以set多个 druidStatPointcut.setPatterns(patterns); return druidStatPointcut; } /** * druid数据库连接池监控 */ @Bean public BeanTypeAutoProxyCreator beanTypeAutoProxyCreator() { BeanTypeAutoProxyCreator beanTypeAutoProxyCreator = new BeanTypeAutoProxyCreator(); beanTypeAutoProxyCreator.setTargetBeanType(DruidDataSource.class); beanTypeAutoProxyCreator.setInterceptorNames("druidStatInterceptor"); return beanTypeAutoProxyCreator; } /** * druid 为druidStatPointcut添加拦截 */ @Bean public Advisor druidStatAdvisor() { return new DefaultPointcutAdvisor(druidStatPointcut(), druidStatInterceptor()); } }4.浏览器访问:http://localhost:8080/druid/
1.添加依赖 2.配置application.yml
1.创建系统用户实体类
@Data public class SysUser { private Long id; private String userName; private String passWord; private Date createTime; private Integer userAge; private String remark; }2.创建系统用户持久层
@Mapper public interface SysUserDao extends BaseMapper<SysUser> { }3.创建系统用户业务层
public interface SysUserService { } @Service("SysUserService") public class SysUserServiceImp extends ServiceImpl<SysUserDao, SysUser> implements SysUserService { }4.创建系统用户控制层
@RestController public class SysUserController { @Autowired SysUserServiceImp sysUserServiceImp; @GetMapping("/getUser") public SysUser getUser(Long id){ return sysUserServiceImp.getById(id); } @PostMapping("/insertUser") public String insertUser(SysUser sysUser){ sysUser.setCreateTime(new Date()); return sysUserServiceImp.save(sysUser)?"保存成功":"保存失败"; } @PutMapping("/updateUser") public String updateUser(SysUser sysUser){ return sysUserServiceImp.updateById(sysUser)?"修改成功":"修改失败"; } @GetMapping("/getList") public List<SysUser> getList(){ return sysUserServiceImp.list(); } }1.添加依赖 2.浏览器访问http://localhost:8080 3.创建配置类
@Configuration @EnableWebSecurity public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/").permitAll() .anyRequest().authenticated() .and() .logout().permitAll() .and() .formLogin(); http.csrf().disable(); } @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers("/js/**", "/css/**", "/images/**"); } }4.添加接口方法
@GetMapping("/") public String index(Long id){ return "欢迎使用!"; }5.创建自定义密码编解码类
public class MyPasswordEncoder implements PasswordEncoder { final static String ENCODER_PWD = "123456"; @Override public String encode(CharSequence rawPassword) { return rawPassword + ENCODER_PWD; } @Override public boolean matches(CharSequence rawPassword, String encodedPassword) { return encodedPassword.equals(rawPassword+ENCODER_PWD); } }修改配置用户密码编解码方式
@Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder()).withUser("admin").password(new MyPasswordEncoder().encode("123456")).roles("ADMIN"); }6.访问http://localhost:8080/getList