我有以下bean
public Sample{
@Value
private String accoutName;
@Value
private String url;
private String password; //
@PostConstruct
public void retrievePassword(){
String serviceURL = url+ "?accountName=" //I have few more paramaters
URL obj = new URL(serviceURL);
HttpsURLConnection connection = null;= (HttpsURLConnection) obj.openConnection();
password = readResponse(connection);//inside this i have bufferreader n all
}
}
}
我解除了HTTP请求并获取响应并将其存储在密码中。
1)如何确保此实例变量是安全的?我试图让它最终但得到错误“可变密码可能尚未初始化”此密码变量将从应用程序上下文中定义的其他bean访问
2)如果我必须设置很多,那么设置Headers的最佳方法是什么...我不想硬编码。感谢
答案 0 :(得分:0)
这里有许多未知数。但是一个好的开始将如下所示:
密码字段不能是最终字段,因为您将从HTTP调用中检索值。更好的选择是将字段设为私有,并为getter提供适当的访问修饰符。
为避免重复HTTP调用,您可以创建实际进行HTTP调用的BaseClass或UtilityClass。在调用此方法时,请发送您的URI和其他POST参数。并使其成为Singleton类。
答案 1 :(得分:0)
您可以使用URI构建器。看一下这个例子:
URI uriBuilder = new URIBuilder()
.setScheme("http")
.setHost(hostVar)
.setPath(pathVar)
.setParameter("parameter1", parameter1Value)
.setParameter("parameter2", parameter2Value)
.build();
建立URL后,您可以获取字符串值以将其用于HTTP调用。
String url = uriBuilder.toString();
答案 2 :(得分:0)
要设置标题,您可以在web.xml中设置过滤器和过滤器映射,如下所示:
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>com.***.CorsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这里,为每个请求调用此过滤器(使用/ *)。您可以指定要调用过滤器的任何自定义路径。
并创建一个过滤器实现,您可以在其中指定所需的所有标头和可接受的请求方法,如下所示:
@Component
public class CorsFilter implements Filter {
private static final Logger logger = LoggerFactory.getLogger(CorsFilter.class);
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
logger.info("Filters CORS request");
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
if(!(request.getMethod().equalsIgnoreCase("PUT")||request.getMethod().equalsIgnoreCase("DELETE"))){
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
response.setHeader("Access-Control-Max-Age", "2592000");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with, content-type, sessionId, x-token,Set-Cookie,Access-Control-Allow-Credentials");
response.setHeader("Set-Cookie", "sessionId=" +request.getSession().getId() +"; path=/appname; secure; HttpOnly; domain="+Constants.DOMAINURL);
response.setHeader("Access-Control-Expose-Headers", "sessionId, x-token");
response.addHeader("Cache-Control", "max-age=2592000");
response.setDateHeader("Expires", System.currentTimeMillis() + 2592000000L); // 1 month in future.
response.setDateHeader("Last-Modified", new Date().getTime());
response.setHeader("X-Frame-Options", "DENY");
response.setHeader("X-XSS-Protection", "1; mode=block");
response.setHeader("X-Content-Type-Options", "nosniff");
boolean isSendRedirect = false;
if( !isSendRedirect)
chain.doFilter(req, res);
}
logger.info("Filters CORS request");
}
public void init(FilterConfig filterConfig) {
}
public void destroy() {
}
}