代码优化

This commit is contained in:
shimingxy
2024-01-20 18:10:17 +08:00
parent a1213a7539
commit b55261ffe4
8 changed files with 24 additions and 36 deletions

View File

@@ -78,11 +78,10 @@ public class LightNoise extends Configurable implements NoiseProducer
while (!pi.isDone())
{
float[] coords = new float[6];
switch (pi.currentSegment(coords))
{
case PathIterator.SEG_MOVETO:
case PathIterator.SEG_LINETO:
switch (pi.currentSegment(coords)){
case PathIterator.SEG_MOVETO,PathIterator.SEG_LINETO:{
tmp[i] = new Point2D.Float(coords[0], coords[1]);
}
}
i++;
pi.next();

View File

@@ -23,12 +23,12 @@ import com.google.code.kaptcha.text.TextProducer;
import com.google.code.kaptcha.util.Configurable;
public class UniqueTextCreator extends Configurable implements TextProducer{
Random rand = new Random();
@Override
public String getText() {
int length = getConfig().getTextProducerCharLength();
char[] chars = getConfig().getTextProducerCharString();
Random rand = new Random();
StringBuffer text = new StringBuffer();
int i = 0;
while ( i < length){

View File

@@ -77,10 +77,7 @@ public class RandomColorWordRenderer extends Configurable implements WordRendere
Font[] chosenFonts = new Font[wordChars.length];
int [] charWidths = new int[wordChars.length];
int widthNeeded = 0;
for (int i = 0; i < wordChars.length; i++)
{
//chosenFonts[i] = new Font("Arial", Font.BOLD, fontSize);
for (int i = 0; i < wordChars.length; i++){
//random
chosenFonts[i] = fonts[random.nextInt(fonts.length)];
@@ -96,14 +93,14 @@ public class RandomColorWordRenderer extends Configurable implements WordRendere
widthNeeded = widthNeeded + charWidths[i];
}
HashMap<String,String> selectedColor =new HashMap<String,String>();
HashMap<String,String> selectedColor =new HashMap<>();
int startPosX = (width - widthNeeded) / 2;
for (int i = 0; i < wordChars.length; i++)
{
String randomcolor="";
do {
randomcolor=COLOR_LIST[random.nextInt(COLOR_LIST.length)].replaceAll(" ", "");
randomcolor=COLOR_LIST[random.nextInt(COLOR_LIST.length)].replace(" ", "");
}while(selectedColor.containsKey(randomcolor));
selectedColor.put(randomcolor, randomcolor);
@@ -117,7 +114,6 @@ public class RandomColorWordRenderer extends Configurable implements WordRendere
wordChars[i]
};
//System.out.println(charToDraw[0] +" - "+chosenFonts[i]);
g2D.drawChars(charToDraw, 0, charToDraw.length, startPosX, startPosY);
startPosX = startPosX + (int) charWidths[i] + charSpace;
}
@@ -125,7 +121,7 @@ public class RandomColorWordRenderer extends Configurable implements WordRendere
return image;
}
static String [] COLOR_LIST = {
static final String [] COLOR_LIST = {
//"255, 255, 255", //white
//"192, 192, 192", //silver
//"128, 128, 128", //gray

View File

@@ -24,7 +24,6 @@ import java.io.IOException;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
@@ -32,19 +31,20 @@ import org.springframework.core.io.Resource;
@AutoConfiguration
public class KaptchaAutoConfiguration implements InitializingBean {
public class KaptchaAutoConfiguration {
private static final Logger _logger = LoggerFactory.getLogger(KaptchaAutoConfiguration.class);
public static final String kaptchaPropertySource = "/kaptcha.properties";
public static final String KAPTCHA_PROPERTY = "/kaptcha.properties";
/**
* Captcha Producer Config .
* @return Producer
* @throws IOException kaptcha.properties is null
*/
@Bean
public Producer captchaProducer() throws IOException {
Resource resource = new ClassPathResource(kaptchaPropertySource);
_logger.debug("Kaptcha config file " + resource.getURL());
Producer captchaProducer() throws IOException {
Resource resource = new ClassPathResource(KAPTCHA_PROPERTY);
_logger.debug("Kaptcha config file {}" , resource.getURL());
DefaultKaptcha kaptcha = new DefaultKaptcha();
Properties properties = new Properties();
properties.load(resource.getInputStream());
@@ -53,8 +53,4 @@ public class KaptchaAutoConfiguration implements InitializingBean {
return kaptcha;
}
@Override
public void afterPropertiesSet() throws Exception {
}
}

View File

@@ -30,7 +30,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@@ -58,7 +58,7 @@ public class ImageCaptchaEndpoint {
* @param request HttpServletRequest
* @param response HttpServletResponse
*/
@RequestMapping(value={"/captcha"}, produces = {MediaType.APPLICATION_JSON_VALUE})
@GetMapping(value={"/captcha"}, produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<?> captchaHandleRequest(
@RequestParam(value="captcha",required=false,defaultValue="text") String captchaType,
@RequestParam(value="state",required=false,defaultValue="state") String state) {
@@ -98,7 +98,7 @@ public class ImageCaptchaEndpoint {
new ImageCaptcha(state,b64Image)
).buildResponse();
} catch (Exception e) {
_logger.error("captcha Producer Error " + e.getMessage());
_logger.error("captcha Producer Error" , e);
}
return new Message< Object>(Message.FAIL).buildResponse();
}

View File

@@ -68,12 +68,11 @@ public class AuthTokenService extends AuthJwtService{
String refreshToken = refreshTokenService.genRefreshToken(authentication);
_logger.trace("generate JWT Token");
String accessToken = genJwt(authentication);
AuthJwt authJwt = new AuthJwt(
return new AuthJwt(
accessToken,
authentication,
authJwkConfig.getExpires(),
refreshToken);
return authJwt;
}
return null;
}
@@ -118,8 +117,7 @@ public class AuthTokenService extends AuthJwtService{
}
public AuthJwt consumeCongress(String congress) {
AuthJwt authJwt = congressService.consume(congress);
return authJwt;
return congressService.consume(congress);
}
public boolean validateCaptcha(String state,String captcha) {

View File

@@ -26,7 +26,7 @@ import com.github.benmanes.caffeine.cache.Caffeine;
public class InMemoryCongressService implements CongressService{
private static final Logger _logger = LoggerFactory.getLogger(InMemoryCongressService.class);
private static final Logger logger = LoggerFactory.getLogger(InMemoryCongressService.class);
protected static Cache<String, AuthJwt> congressStore =
Caffeine.newBuilder()
@@ -52,8 +52,7 @@ public class InMemoryCongressService implements CongressService{
@Override
public AuthJwt get(String congress) {
AuthJwt authJwt = congressStore.getIfPresent(congress);
return authJwt;
return congressStore.getIfPresent(congress);
}
@Override

View File

@@ -24,13 +24,13 @@ import org.slf4j.LoggerFactory;
public class RedisCongressService implements CongressService {
private static final Logger _logger = LoggerFactory.getLogger(RedisCongressService.class);
private static final Logger logger = LoggerFactory.getLogger(RedisCongressService.class);
protected int validitySeconds = 60 * 3; //default 3 minutes.
RedisConnectionFactory connectionFactory;
public static String PREFIX="REDIS_CONGRESS_";
public static final String PREFIX = "REDIS:CONGRESS:";
/**
* @param connectionFactory
*/