diff --git a/mall-admin/pom.xml b/mall-admin/pom.xml index 10a34f0..e3df15e 100644 --- a/mall-admin/pom.xml +++ b/mall-admin/pom.xml @@ -25,24 +25,47 @@ mall-mbg 1.0-SNAPSHOT + + org.springframework.boot + spring-boot-starter-thymeleaf + org.springframework.boot spring-boot-starter-web org.springframework.boot - spring-boot-starter-thymeleaf + spring-boot-starter-security org.mybatis.spring.boot mybatis-spring-boot-starter 1.2.0 + + org.springframework.boot + spring-boot-starter-actuator + org.springframework.boot spring-boot-starter-test test + + io.springfox + springfox-swagger2 + 2.6.1 + + + io.springfox + springfox-swagger-ui + 2.6.1 + + + com.github.pagehelper + pagehelper-spring-boot-starter + 1.2.3 + diff --git a/mall-admin/src/main/java/com/macro/mall/bo/AdminUserDetails.java b/mall-admin/src/main/java/com/macro/mall/bo/AdminUserDetails.java new file mode 100644 index 0000000..4e12df8 --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/bo/AdminUserDetails.java @@ -0,0 +1,56 @@ +package com.macro.mall.bo; + +import com.macro.mall.model.UmsAdmin; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; + +import java.util.Arrays; +import java.util.Collection; + +/** + * SpringSecurity需要的用户详情 + */ +public class AdminUserDetails implements UserDetails { + private UmsAdmin umsAdmin; + + public AdminUserDetails(UmsAdmin umsAdmin) { + this.umsAdmin = umsAdmin; + } + + @Override + public Collection getAuthorities() { + //返回当前用户的权限 + return Arrays.asList(new SimpleGrantedAuthority("TEST")); + } + + @Override + public String getPassword() { + return umsAdmin.getPassword(); + } + + @Override + public String getUsername() { + return umsAdmin.getUsername(); + } + + @Override + public boolean isAccountNonExpired() { + return true; + } + + @Override + public boolean isAccountNonLocked() { + return true; + } + + @Override + public boolean isCredentialsNonExpired() { + return true; + } + + @Override + public boolean isEnabled() { + return true; + } +} diff --git a/mall-admin/src/main/java/com/macro/mall/config/SecurityConfig.java b/mall-admin/src/main/java/com/macro/mall/config/SecurityConfig.java new file mode 100644 index 0000000..c083716 --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/config/SecurityConfig.java @@ -0,0 +1,73 @@ +package com.macro.mall.config; + +import com.macro.mall.bo.AdminUserDetails; +import com.macro.mall.model.UmsAdmin; +import com.macro.mall.service.UmsAdminService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.authentication.encoding.Md5PasswordEncoder; +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.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; + + +/** + * SpringSecurity的配置 + */ +@Configuration +@EnableWebSecurity +public class SecurityConfig extends WebSecurityConfigurerAdapter { + @Autowired + private UmsAdminService adminService; + + @Override + protected void configure(HttpSecurity http) throws Exception { + http.authorizeRequests()//配置权限 +// .antMatchers("/").access("hasRole('TEST')")//该路径需要TEST角色 +// .antMatchers("/brand/list").authenticated()//该路径需要登录认证 +// .antMatchers("/brand/list").hasAuthority("TEST")//该路径需要TEST权限 + .antMatchers("/**").permitAll() + .and()//启用基于http的认证 + .httpBasic() + .realmName("/") + .and()//配置登录页面 + .formLogin() + .loginPage("/login") + .failureUrl("/login?error=true") + .and()//配置退出路径 + .logout() + .logoutSuccessUrl("/") +// .and()//记住密码功能 +// .rememberMe() +// .tokenValiditySeconds(60*60*24) +// .key("rememberMeKey") + .and()//关闭跨域伪造 + .csrf() + .disable(); + } + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.userDetailsService(userDetailsService()).passwordEncoder(new Md5PasswordEncoder()); + } + + @Bean + public UserDetailsService userDetailsService() { + //获取登录用户信息 + return new UserDetailsService() { + @Override + public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { + UmsAdmin admin = adminService.getAdminByUsername(username); + if(admin!=null){ + return new AdminUserDetails(admin); + } + throw new UsernameNotFoundException("用户名或密码错误"); + } + }; + } +} diff --git a/mall-admin/src/main/java/com/macro/mall/config/Swagger2Config.java b/mall-admin/src/main/java/com/macro/mall/config/Swagger2Config.java new file mode 100644 index 0000000..5e087d0 --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/config/Swagger2Config.java @@ -0,0 +1,37 @@ +package com.macro.mall.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.service.ApiInfo; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +/** + * Swagger2API文档的配置 + */ +@Configuration +@EnableSwagger2 +public class Swagger2Config { + @Bean + public Docket createRestApi(){ + return new Docket(DocumentationType.SWAGGER_2) + .apiInfo(apiInfo()) + .select() + .apis(RequestHandlerSelectors.basePackage("com.macro.mall.controller")) + .paths(PathSelectors.any()) + .build(); + } + + private ApiInfo apiInfo() { + return new ApiInfoBuilder() + .title("mall后台系统") + .description("mall后台模块") + .contact("macro") + .version("1.0") + .build(); + } +} diff --git a/mall-admin/src/main/java/com/macro/mall/controller/PmsBrandController.java b/mall-admin/src/main/java/com/macro/mall/controller/PmsBrandController.java new file mode 100644 index 0000000..2049044 --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/controller/PmsBrandController.java @@ -0,0 +1,98 @@ +package com.macro.mall.controller; + +import com.macro.mall.dto.CommonResult; +import com.macro.mall.dto.PmsBrandDto; +import com.macro.mall.service.PmsBrandService; +import io.swagger.annotations.ApiOperation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.validation.BindingResult; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +/** + * 品牌功能Controller + */ +@Controller +public class PmsBrandController { + @Autowired + private PmsBrandService brandService; + + private static final Logger LOGGER = LoggerFactory.getLogger(PmsBrandController.class); + + @ApiOperation(value = "获取全部品牌列表") + @RequestMapping(value = "/brand/listAll", method = RequestMethod.GET) + @ResponseBody + public Object getBrandList() { + return new CommonResult().success(brandService.listAllBrand()); + } + + @ApiOperation(value = "添加品牌") + @RequestMapping(value = "/brand/create", method = RequestMethod.POST) + @ResponseBody + public Object createBrand(@Validated @RequestBody PmsBrandDto pmsBrand, BindingResult result) { + if (result.hasErrors()) { + return new CommonResult().validateFailed(result.getFieldError().getDefaultMessage()); + } + CommonResult commonResult; + int count = brandService.createBrand(pmsBrand); + if (count == 1) { + commonResult = new CommonResult().success(pmsBrand); + LOGGER.debug("createBrand success:{}", pmsBrand); + } else { + commonResult = new CommonResult().failed(); + LOGGER.debug("createBrand failed:{}", pmsBrand); + } + return commonResult; + } + + @ApiOperation(value = "更新品牌") + @RequestMapping(value = "/brand/update/{id}", method = RequestMethod.POST) + @ResponseBody + public Object updateBrand(@PathVariable("id") Long id, @Validated @RequestBody PmsBrandDto pmsBrandDto, BindingResult result) { + if(result.hasErrors()){ + return new CommonResult().validateFailed(result.getFieldError().getDefaultMessage()); + } + CommonResult commonResult; + int count = brandService.updateBrand(id, pmsBrandDto); + if (count == 1) { + commonResult = new CommonResult().success(pmsBrandDto); + LOGGER.debug("updateBrand success:{}", pmsBrandDto); + } else { + commonResult = new CommonResult().failed(); + LOGGER.debug("updateBrand failed:{}", pmsBrandDto); + } + return commonResult; + } + + @ApiOperation(value = "删除品牌") + @RequestMapping(value = "/brand/delete/{id}", method = RequestMethod.GET) + @ResponseBody + public Object deleteBrand(@PathVariable("id") Long id) { + int count = brandService.deleteBrand(id); + if (count == 1) { + LOGGER.debug("deleteBrand success :id={}", id); + return new CommonResult().success(null); + } else { + LOGGER.debug("deleteBrand failed :id={}", id); + return new CommonResult().failed(); + } + } + + @ApiOperation(value = "分页获取品牌列表") + @RequestMapping(value = "/brand/list", method = RequestMethod.GET) + @ResponseBody + public Object listBrand(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, + @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) { + return new CommonResult().pageSuccess(brandService.listBrand(pageNum, pageSize)); + } + + @ApiOperation(value = "根据编号查询品牌信息") + @RequestMapping(value = "/brand/{id}", method = RequestMethod.GET) + @ResponseBody + public Object getBrand(@PathVariable("id") Long id) { + return new CommonResult().success(brandService.getBrand(id)); + } +} diff --git a/mall-demo/src/main/java/com/macro/mall/demo/bo/CommonResult.java b/mall-admin/src/main/java/com/macro/mall/dto/CommonResult.java similarity index 98% rename from mall-demo/src/main/java/com/macro/mall/demo/bo/CommonResult.java rename to mall-admin/src/main/java/com/macro/mall/dto/CommonResult.java index 98fbce2..ee718df 100644 --- a/mall-demo/src/main/java/com/macro/mall/demo/bo/CommonResult.java +++ b/mall-admin/src/main/java/com/macro/mall/dto/CommonResult.java @@ -1,4 +1,4 @@ -package com.macro.mall.demo.bo; +package com.macro.mall.dto; import com.github.pagehelper.PageInfo; diff --git a/mall-admin/src/main/java/com/macro/mall/dto/PmsBrandDto.java b/mall-admin/src/main/java/com/macro/mall/dto/PmsBrandDto.java new file mode 100644 index 0000000..96c5e68 --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/dto/PmsBrandDto.java @@ -0,0 +1,100 @@ +package com.macro.mall.dto; + +import com.macro.mall.validator.FlagValidator; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import javax.validation.constraints.Min; +import javax.validation.constraints.NotNull; + +/** + * 品牌传递参数 + */ +@ApiModel(value = "PmsBrandDto") +public class PmsBrandDto { + @ApiModelProperty(value = "品牌名称",required = true) + @NotNull(message = "名称不能为空") + private String name; + @ApiModelProperty(value = "品牌首字母") + private String firstLetter; + @ApiModelProperty(value = "排序字段") + @Min(value = 0, message = "排序最小为0") + private Integer sort; + @ApiModelProperty(value = "是否为厂家制造商") + @FlagValidator(value = {"0","1"}, message = "厂家状态不正确") + private Integer factoryStatus; + @ApiModelProperty(value = "是否进行显示") + @FlagValidator(value = {"0","1"}, message = "显示状态不正确") + private Integer showStatus; + @ApiModelProperty(value = "品牌logo",required = true) + @NotNull(message = "品牌logo不能为空") + private String logo; + @ApiModelProperty(value = "品牌大图") + private String bigPic; + @ApiModelProperty(value = "品牌故事") + private String brandStory; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getFirstLetter() { + return firstLetter; + } + + public void setFirstLetter(String firstLetter) { + this.firstLetter = firstLetter; + } + + public Integer getSort() { + return sort; + } + + public void setSort(Integer sort) { + this.sort = sort; + } + + public Integer getFactoryStatus() { + return factoryStatus; + } + + public void setFactoryStatus(Integer factoryStatus) { + this.factoryStatus = factoryStatus; + } + + public Integer getShowStatus() { + return showStatus; + } + + public void setShowStatus(Integer showStatus) { + this.showStatus = showStatus; + } + + public String getLogo() { + return logo; + } + + public void setLogo(String logo) { + this.logo = logo; + } + + public String getBigPic() { + return bigPic; + } + + public void setBigPic(String bigPic) { + this.bigPic = bigPic; + } + + public String getBrandStory() { + return brandStory; + } + + public void setBrandStory(String brandStory) { + this.brandStory = brandStory; + } +} diff --git a/mall-admin/src/main/java/com/macro/mall/service/PmsBrandService.java b/mall-admin/src/main/java/com/macro/mall/service/PmsBrandService.java new file mode 100644 index 0000000..13517b6 --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/service/PmsBrandService.java @@ -0,0 +1,23 @@ +package com.macro.mall.service; + +import com.macro.mall.dto.PmsBrandDto; +import com.macro.mall.model.PmsBrand; + +import java.util.List; + +/** + * 商品品牌Service + */ +public interface PmsBrandService { + List listAllBrand(); + + int createBrand(PmsBrandDto pmsBrandDto); + + int updateBrand(Long id, PmsBrandDto pmsBrandDto); + + int deleteBrand(Long id); + + List listBrand(int pageNum, int pageSize); + + PmsBrand getBrand(Long id); +} diff --git a/mall-admin/src/main/java/com/macro/mall/service/UmsAdminService.java b/mall-admin/src/main/java/com/macro/mall/service/UmsAdminService.java new file mode 100644 index 0000000..56cf878 --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/service/UmsAdminService.java @@ -0,0 +1,13 @@ +package com.macro.mall.service; + +import com.macro.mall.model.UmsAdmin; + +/** + * 后台管理员Service + */ +public interface UmsAdminService { + /** + * 根据用户名获取后台管理员 + */ + UmsAdmin getAdminByUsername(String username); +} diff --git a/mall-admin/src/main/java/com/macro/mall/service/impl/PmsBrandServiceImpl.java b/mall-admin/src/main/java/com/macro/mall/service/impl/PmsBrandServiceImpl.java new file mode 100644 index 0000000..88fea94 --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/service/impl/PmsBrandServiceImpl.java @@ -0,0 +1,58 @@ +package com.macro.mall.service.impl; + +import com.github.pagehelper.PageHelper; +import com.macro.mall.dto.PmsBrandDto; +import com.macro.mall.mapper.PmsBrandMapper; +import com.macro.mall.model.PmsBrand; +import com.macro.mall.model.PmsBrandExample; +import com.macro.mall.service.PmsBrandService; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * 商品品牌Service实现类 + */ +@Service +public class PmsBrandServiceImpl implements PmsBrandService{ + @Autowired + private PmsBrandMapper brandMapper; + + @Override + public List listAllBrand() { + return brandMapper.selectByExample(new PmsBrandExample()); + } + + @Override + public int createBrand(PmsBrandDto pmsBrandDto) { + PmsBrand pmsBrand = new PmsBrand(); + BeanUtils.copyProperties(pmsBrandDto,pmsBrand); + return brandMapper.insertSelective(pmsBrand); + } + + @Override + public int updateBrand(Long id, PmsBrandDto pmsBrandDto) { + PmsBrand pmsBrand = new PmsBrand(); + BeanUtils.copyProperties(pmsBrandDto,pmsBrand); + pmsBrand.setId(id); + return brandMapper.updateByPrimaryKeySelective(pmsBrand); + } + + @Override + public int deleteBrand(Long id) { + return brandMapper.deleteByPrimaryKey(id); + } + + @Override + public List listBrand(int pageNum, int pageSize) { + PageHelper.startPage(pageNum, pageSize); + return brandMapper.selectByExample(new PmsBrandExample()); + } + + @Override + public PmsBrand getBrand(Long id) { + return brandMapper.selectByPrimaryKey(id); + } +} diff --git a/mall-admin/src/main/java/com/macro/mall/service/impl/UmsAdminServiceImpl.java b/mall-admin/src/main/java/com/macro/mall/service/impl/UmsAdminServiceImpl.java new file mode 100644 index 0000000..0d34b82 --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/service/impl/UmsAdminServiceImpl.java @@ -0,0 +1,29 @@ +package com.macro.mall.service.impl; + +import com.macro.mall.mapper.UmsAdminMapper; +import com.macro.mall.model.UmsAdmin; +import com.macro.mall.model.UmsAdminExample; +import com.macro.mall.service.UmsAdminService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * UmsAdminService实现类 + */ +@Service +public class UmsAdminServiceImpl implements UmsAdminService{ + @Autowired + private UmsAdminMapper adminMapper; + @Override + public UmsAdmin getAdminByUsername(String username) { + UmsAdminExample example = new UmsAdminExample(); + example.createCriteria().andUsernameEqualTo(username); + List adminList = adminMapper.selectByExample(example); + if(adminList!=null&&adminList.size()>0){ + return adminList.get(0); + } + return null; + } +} diff --git a/mall-admin/src/main/java/com/macro/mall/validator/FlagValidator.java b/mall-admin/src/main/java/com/macro/mall/validator/FlagValidator.java new file mode 100644 index 0000000..585c51d --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/validator/FlagValidator.java @@ -0,0 +1,22 @@ +package com.macro.mall.validator; + +import javax.validation.Constraint; +import javax.validation.Payload; +import java.lang.annotation.*; + +/** + * 用户验证状态是否在指定范围内的注解 + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.FIELD,ElementType.PARAMETER}) +@Constraint(validatedBy = FlagValidatorClass.class) +public @interface FlagValidator { + String[] value() default {}; + + String message() default "flag is not found"; + + Class[] groups() default {}; + + Class[] payload() default {}; +} diff --git a/mall-admin/src/main/java/com/macro/mall/validator/FlagValidatorClass.java b/mall-admin/src/main/java/com/macro/mall/validator/FlagValidatorClass.java new file mode 100644 index 0000000..30f8b06 --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/validator/FlagValidatorClass.java @@ -0,0 +1,27 @@ +package com.macro.mall.validator; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; + +/** + * 状态标记校验器 + */ +public class FlagValidatorClass implements ConstraintValidator { + private String[] values; + @Override + public void initialize(FlagValidator flagValidator) { + this.values = flagValidator.value(); + } + + @Override + public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) { + boolean isValid = false; + for(int i=0;i result = new HashMap<>(); + result.put("pageSize", pageInfo.getPageSize()); + result.put("totalPage", totalPage); + result.put("pageNum", pageInfo.getPageNum()); + result.put("list", pageInfo.getList()); + this.code = SUCCESS; + this.message = "操作成功"; + this.data = result; + return this; + } + + /** + * 普通失败提示信息 + */ + public CommonResult failed() { + this.code = FAILED; + this.message = "操作失败"; + return this; + } + + /** + * 参数验证失败使用 + * + * @param message 错误信息 + */ + public CommonResult validateFailed(String message) { + this.code = VALIDATE_FAILED; + this.message = message; + return this; + } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } +} diff --git a/mall-demo/src/main/java/com/macro/mall/demo/dto/PmsBrandDto.java b/mall-demo/src/main/java/com/macro/mall/demo/dto/PmsBrandDto.java index d26c791..933c2c1 100644 --- a/mall-demo/src/main/java/com/macro/mall/demo/dto/PmsBrandDto.java +++ b/mall-demo/src/main/java/com/macro/mall/demo/dto/PmsBrandDto.java @@ -22,10 +22,10 @@ public class PmsBrandDto { @Min(value = 0, message = "排序最小为0") private Integer sort; @ApiModelProperty(value = "是否为厂家制造商") - @FlagValidator(values = {"0","1"}, message = "厂家状态不正确") + @FlagValidator(value = {"0","1"}, message = "厂家状态不正确") private Integer factoryStatus; @ApiModelProperty(value = "是否进行显示") - @FlagValidator(values = {"0","1"}, message = "显示状态不正确") + @FlagValidator(value = {"0","1"}, message = "显示状态不正确") private Integer showStatus; @ApiModelProperty(value = "品牌logo") private String logo; diff --git a/mall-demo/src/main/java/com/macro/mall/demo/validator/FlagValidator.java b/mall-demo/src/main/java/com/macro/mall/demo/validator/FlagValidator.java index eda4511..b9bdaed 100644 --- a/mall-demo/src/main/java/com/macro/mall/demo/validator/FlagValidator.java +++ b/mall-demo/src/main/java/com/macro/mall/demo/validator/FlagValidator.java @@ -12,7 +12,7 @@ import java.lang.annotation.*; @Target({ElementType.FIELD,ElementType.PARAMETER}) @Constraint(validatedBy = FlagValidatorClass.class) public @interface FlagValidator { - String[] values() default {}; + String[] value() default {}; String message() default "flag is not found";