Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiesun committed May 23, 2019
2 parents f27d75b + 77d8411 commit 5626b53
Show file tree
Hide file tree
Showing 19 changed files with 194 additions and 111 deletions.
5 changes: 3 additions & 2 deletions src/main/java/org/toughradius/component/LangResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@
import org.toughradius.common.ValidateUtil;
import org.toughradius.config.LangElement;

import javax.annotation.PostConstruct;
import java.io.*;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
public class LangResources {

private Map<String,LangElement> langMap = new HashMap<String, LangElement>();

public LangResources() {
@PostConstruct
public void LangResources() {
try {
InputStream fis = LangResources.class.getClassLoader().getResourceAsStream("lang_resource.json");
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
Expand Down
56 changes: 29 additions & 27 deletions src/main/java/org/toughradius/config/AccessInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.toughradius.common.RestResult;
import org.toughradius.common.ValidateUtil;
import org.toughradius.common.coder.Base64;
import org.toughradius.component.ConfigService;
import org.toughradius.component.LangResources;
import org.toughradius.component.Memarylogger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

@Configuration
public class AccessInterceptor extends HandlerInterceptorAdapter {
public class AccessInterceptor extends HandlerInterceptorAdapter implements Constant {

@Autowired
protected Memarylogger logger;
Expand All @@ -31,36 +31,38 @@ public class AccessInterceptor extends HandlerInterceptorAdapter {
@Autowired
protected ApplicationConfig appConfig;

@Autowired
protected ConfigService cfgService;


@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String header = request.getHeader("Authorization");
response.setContentType("application/json;charset=UTF-8");
if (handler instanceof HandlerMethod){
String header = request.getHeader("Authorization");
if(ValidateUtil.isNotEmpty(header) && !header.substring(0, 6).equals("Basic ")){
response.getWriter().print(gson.toJson(langs.tr("未支持的验证方式",request.getHeader("Accept-Language"))));
return false;
}
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
ApiAccess access = method.getAnnotation(ApiAccess.class);
if(access!=null){
String basicAuthEncoded = header.substring(6);
//will contain "bob:secret"
String basicAuthAsString = new String(new Base64().decode(basicAuthEncoded.getBytes()));
if(!basicAuthAsString.trim().equals(String.format("%s:%s", appConfig.getApikey(),appConfig.getApisecret()))){
response.getWriter().print(gson.toJson(langs.tr("未授权的操作",request.getHeader("Accept-Language"))));
return false;
}else{
return true;
}
}else {
return true;
}
}else {
if(ValidateUtil.isEmpty(header)){
response.setCharacterEncoding("UTF-8");
response.setHeader("Authorization","Required");
response.setHeader("WWW-Authentication ","Basic");
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
response.getWriter().print(gson.toJson(new RestResult(1,"Forbidden, unauthorized user")));
return false;
}
if(ValidateUtil.isNotEmpty(header) && !header.substring(0, 6).equals("Basic ")){
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
response.getWriter().print(gson.toJson(new RestResult(1,"Unsupported authentication methods")));
return false;
}
String basicAuthEncoded = header.substring(6);
//will contain "bob:secret"
String basicAuthAsString = new String(new Base64().decode(basicAuthEncoded.getBytes()));
if(!basicAuthAsString.trim().equals(String.format("%s:%s",
cfgService.getStringValue(API_MODULE,API_USERNAME),
cfgService.getStringValue(API_MODULE,API_PASSWD)))){
response.getWriter().print(gson.toJson(new RestResult(1,"Authentication failure")));
return false;
}else{
return true;
}

}

@Override
Expand Down
11 changes: 0 additions & 11 deletions src/main/java/org/toughradius/config/ApiAccess.java

This file was deleted.

5 changes: 5 additions & 0 deletions src/main/java/org/toughradius/config/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public interface Constant {
public final static String SYSTEM_USERNAME = "systemUsername";
public final static String SYSTEM_USERPWD = "systemUserpwd";

public final static String API_MODULE = "api";
public final static String API_TYPE = "apiType";
public final static String API_USERNAME = "apiUsername";
public final static String API_PASSWD = "apiPasswd";

public final static String SMS_MODULE = "sms";
public final static String SMS_GATEWAY = "smsGateway";
public final static String SMS_APPID = "smsAppid";
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/toughradius/config/MvcConfigurer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public class MvcConfigurer extends WebMvcConfigurerAdapter {
@Autowired
private PortalConfig portalConfig;

@Autowired
private AccessInterceptor accessInterceptor;


@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/error").setViewName("/templates/global_error.html");
Expand All @@ -30,7 +34,7 @@ public void configurePathMatch(PathMatchConfigurer configurer) {
}

public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new AccessInterceptor()).addPathPatterns("/api/**");
registry.addInterceptor(accessInterceptor).addPathPatterns("/api/v6/**");
registry.addInterceptor(new SessionInterceptor()).addPathPatterns("/admin/**")
.excludePathPatterns("/")
.excludePathPatterns("/admin")
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/toughradius/controller/BrasController.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class BrasController {
@Autowired
private BrasService brasService;

@GetMapping(value = {"/admin/bras/query"})
@GetMapping(value = {"/api/v6/bras/query","/admin/bras/query"})
@ResponseBody
public List<Bras> queryBras(){
List<Bras> result = new ArrayList<Bras>();
Expand All @@ -36,7 +36,7 @@ public List<Bras> queryBras(){
return result;
}

@PostMapping(value = {"/admin/bras/create"})
@PostMapping(value = {"/api/v6/bras/create","/admin/bras/create"})
@ResponseBody
public RestResult addBras(Bras bras){
try{
Expand All @@ -58,7 +58,7 @@ public RestResult addBras(Bras bras){
}
}

@PostMapping(value = {"/admin/bras/update"})
@PostMapping(value = {"/api/v6/bras/update","/admin/bras/update"})
@ResponseBody
public RestResult updateBras(Bras bras){
try{
Expand All @@ -73,7 +73,7 @@ public RestResult updateBras(Bras bras){
}
}

@GetMapping(value = {"/admin/bras/delete"})
@GetMapping(value = {"/api/v6/bras/delete","/admin/bras/delete"})
@ResponseBody
public RestResult deleteBras(Long id){
try{
Expand Down
27 changes: 23 additions & 4 deletions src/main/java/org/toughradius/controller/ConfigController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.toughradius.component.Memarylogger;
import org.toughradius.config.Constant;
import org.toughradius.entity.Config;
import org.toughradius.form.ApiConfigForm;
import org.toughradius.form.RadiusConfigForm;
import org.toughradius.form.SmsConfigForm;
import org.toughradius.form.WlanCongigForm;
Expand All @@ -31,7 +32,7 @@ public class ConfigController implements Constant {
@Autowired
private ConfigService configService;

@GetMapping(value = {"/admin/config/load/{module}"})
@GetMapping(value = {"/api/v6/config/load/{module}","/admin/config/load/{module}"})
@ResponseBody
public Map loadRadiusConfig(@PathVariable(name = "module")String module){
Map result = new HashMap();
Expand All @@ -51,7 +52,7 @@ public Map loadRadiusConfig(@PathVariable(name = "module")String module){
* @param form
* @return
*/
@PostMapping(value = {"/admin/config/radius/update"})
@PostMapping(value = {"/api/v6/radius/update","/admin/config/radius/update"})
@ResponseBody
public RestResult updateRadiusConfig(RadiusConfigForm form){
try{
Expand All @@ -70,7 +71,7 @@ public RestResult updateRadiusConfig(RadiusConfigForm form){
* @param form
* @return
*/
@PostMapping(value = {"/admin/config/sms/update"})
@PostMapping(value = {"/api/v6/sms/update","/admin/config/sms/update"})
@ResponseBody
public RestResult updateSmsConfig(SmsConfigForm form){
try{
Expand All @@ -84,13 +85,31 @@ public RestResult updateSmsConfig(SmsConfigForm form){
return new RestResult(0,"update sms config done");
}

/**
* API 配置更新呢
* @param form
* @return
*/
@PostMapping(value = {"/admin/config/api/update"})
@ResponseBody
public RestResult updateApiConfig(ApiConfigForm form){
try{
configService.updateConfig(new Config(API_MODULE,API_TYPE,form.getApiType()));
configService.updateConfig(new Config(API_MODULE,API_USERNAME,form.getApiUsername()));
configService.updateConfig(new Config(API_MODULE,API_PASSWD,form.getApiPasswd()));
}catch(Exception e){
logger.error("update config error",e, Memarylogger.SYSTEM);
}
return new RestResult(0,"update api config done");
}


/**
* 无线认证配置更新
* @param form
* @return
*/
@PostMapping(value = {"/admin/config/wlan/update"})
@PostMapping(value = {"/api/v6/wlan/update","/admin/config/wlan/update"})
@ResponseBody
public RestResult updateWlanConfig(WlanCongigForm form){
try{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,43 +31,43 @@ public class DashboardController {
@Autowired
private RadiusOnlineStat radiusOnlineStat;

@GetMapping("/admin/radius/stat")
@GetMapping({"/api/v6/radius/stat","/admin/radius/stat"})
@ResponseBody
public Map queryRadiusStat(){
return radiusStat.getData();
}

@GetMapping({"/radius/caststat","/admin/radius/caststat"})
@GetMapping({"/api/v6/radius/caststat","/admin/radius/caststat"})
@ResponseBody
public Map queryRadiusCastStat(){
return radiusCastStat.getData();
}

@GetMapping({"/radius/authstat","/admin/radius/authstat"})
@GetMapping({"/api/v6/radius/authstat","/admin/radius/authstat"})
@ResponseBody
public Map queryRadiusAuthStat(){
return radiusAuthStat.getData();
}

@GetMapping({"/radius/onlinestat","/admin/radius/onlinestat"})
@GetMapping({"/api/v6/radius/onlinestat","/admin/radius/onlinestat"})
@ResponseBody
public Map queryRadiusOnlineStat(){
return radiusOnlineStat.getData();
}

@GetMapping(value = {"/admin/dashboard/cpuuse"})
@GetMapping({"/api/v6/cpuuse","/admin/dashboard/cpuuse"})
@ResponseBody
public RestResult cpuuse(){
return new RestResult(0,"ok", SystemUtil.getCpuUsage());
}

@GetMapping(value = {"/admin/dashboard/memuse"})
@GetMapping(value = {"/api/v6/memuse","/admin/dashboard/memuse"})
@ResponseBody
public RestResult memuse(){
return new RestResult(0,"ok", SystemUtil.getMemUsage());
}

@GetMapping(value = {"/admin/dashboard/diskuse"})
@GetMapping({"/api/v6/diskuse","/admin/dashboard/diskuse"})
@ResponseBody
public RestResult diskuse(){
try {
Expand All @@ -78,7 +78,7 @@ public RestResult diskuse(){
}
}

@GetMapping(value = {"/admin/dashboard/uptime"})
@GetMapping({"/admin/dashboard/uptime"})
@ResponseBody
public String uptime(){
return String.format("<i class='fa fa-bar-chart'></i> 应用系统运行时长 %s ", DateTimeUtil.formatSecond(SystemUtil.getUptime()/1000));
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/toughradius/controller/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ public RestResult sessionHandeler(HttpSession session, HttpServletRequest reques
@ResponseBody
public RestResult loginHandler(String username, String password, HttpSession session) {
try {
String sysUserName = configService.getStringValue(ConfigService.SYSTEM_MODULE,ConfigService.SYSTEM_USERNAME);
String sysUserPwd = configService.getStringValue(ConfigService.SYSTEM_MODULE,ConfigService.SYSTEM_USERPWD);
String sysUserName = configService.getStringValue(SYSTEM_MODULE,SYSTEM_USERNAME);
String sysUserPwd = configService.getStringValue(SYSTEM_MODULE,SYSTEM_USERPWD);
if(ValidateUtil.isEmpty(sysUserName)){
sysUserName = "admin";
configService.updateConfig(new Config(ConfigService.SYSTEM_MODULE,ConfigService.SYSTEM_USERNAME,sysUserName,""));
configService.updateConfig(new Config(SYSTEM_MODULE,SYSTEM_USERNAME,sysUserName,""));
}
if(ValidateUtil.isEmpty(sysUserPwd)){
sysUserPwd = CoderUtil.md5Salt("root");
configService.updateConfig(new Config(ConfigService.SYSTEM_MODULE,ConfigService.SYSTEM_USERPWD,sysUserPwd,""));
configService.updateConfig(new Config(SYSTEM_MODULE,SYSTEM_USERPWD,sysUserPwd,""));
}

if(username.equals(sysUserName) && CoderUtil.md5Salt(password).equals(sysUserPwd)){
Expand Down
Loading

0 comments on commit 5626b53

Please sign in to comment.