commit
8abb2ee012
|
@ -0,0 +1,29 @@
|
|||
=========
|
||||
|
||||
## Login and Registration Example Project with Spring Security
|
||||
|
||||
|
||||
### Relevant Articles:
|
||||
- [Spring Security Registration Tutorial](http://www.baeldung.com/spring-security-registration)
|
||||
|
||||
|
||||
|
||||
### Build the Project
|
||||
```
|
||||
mvn clean install
|
||||
```
|
||||
|
||||
|
||||
### Set up MySQL
|
||||
```
|
||||
mysql -u root -p
|
||||
> CREATE USER 'tutorialuser'@'localhost' IDENTIFIED BY 'tutorialmy5ql';
|
||||
> GRANT ALL PRIVILEGES ON *.* TO 'tutorialuser'@'localhost';
|
||||
> FLUSH PRIVILEGES;
|
||||
```
|
||||
|
||||
|
||||
### Set up Email
|
||||
|
||||
You need to configure the email by renaming file "email.properties.sample" to "email.properties" and provide your own username and password.
|
||||
You also need to use your own host, you can use Amazon or Google for example.
|
|
@ -1,9 +1,13 @@
|
|||
package org.baeldung.spring;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("org.baeldung.web")
|
||||
|
@ -14,6 +18,18 @@ public class WebConfig extends WebMvcConfigurerAdapter {
|
|||
super();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ViewResolver viewResolver() {
|
||||
final InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
|
||||
viewResolver.setPrefix("/WEB-INF/view/");
|
||||
viewResolver.setSuffix(".jsp");
|
||||
return viewResolver;
|
||||
}
|
||||
// API
|
||||
@Override
|
||||
public void addViewControllers(final ViewControllerRegistry registry) {
|
||||
super.addViewControllers(registry);
|
||||
registry.addViewController("/graph.html");
|
||||
}
|
||||
|
||||
}
|
|
@ -51,4 +51,14 @@ public class RootController {
|
|||
public String getStatusMetric() {
|
||||
return metricService.getStatusMetric();
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/metric-graph", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public Object[][] drawMetric() {
|
||||
final Object[][] result = metricService.getGraphData();
|
||||
for (int i = 1; i < result[0].length; i++) {
|
||||
result[0][i] = result[0][i].toString();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
package org.baeldung.web.metric;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
@ -9,12 +13,20 @@ public class MetricService {
|
|||
|
||||
private static HashMap<String, HashMap<Integer, Integer>> metricMap = new HashMap<String, HashMap<Integer, Integer>>();
|
||||
private static HashMap<Integer, Integer> statusMetric = new HashMap<Integer, Integer>();
|
||||
private static HashMap<String, HashMap<Integer, Integer>> timeMap = new HashMap<String, HashMap<Integer, Integer>>();
|
||||
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
||||
|
||||
public MetricService() {
|
||||
super();
|
||||
}
|
||||
|
||||
public void increaseCount(final String request, final int status) {
|
||||
increaseMainMetric(request, status);
|
||||
increaseStatusMetric(status);
|
||||
updateTimeMap(status);
|
||||
}
|
||||
|
||||
private void increaseMainMetric(final String request, final int status) {
|
||||
HashMap<Integer, Integer> statusMap = metricMap.get(request);
|
||||
if (statusMap == null) {
|
||||
statusMap = new HashMap<Integer, Integer>();
|
||||
|
@ -28,7 +40,9 @@ public class MetricService {
|
|||
}
|
||||
statusMap.put(status, count);
|
||||
metricMap.put(request, statusMap);
|
||||
}
|
||||
|
||||
private void increaseStatusMetric(final int status) {
|
||||
final Integer statusCount = statusMetric.get(status);
|
||||
if (statusCount == null) {
|
||||
statusMetric.put(status, 1);
|
||||
|
@ -37,6 +51,23 @@ public class MetricService {
|
|||
}
|
||||
}
|
||||
|
||||
private void updateTimeMap(final int status) {
|
||||
final String time = dateFormat.format(new Date());
|
||||
HashMap<Integer, Integer> statusMap = timeMap.get(time);
|
||||
if (statusMap == null) {
|
||||
statusMap = new HashMap<Integer, Integer>();
|
||||
}
|
||||
|
||||
Integer count = statusMap.get(status);
|
||||
if (count == null) {
|
||||
count = 1;
|
||||
} else {
|
||||
count++;
|
||||
}
|
||||
statusMap.put(status, count);
|
||||
timeMap.put(time, statusMap);
|
||||
}
|
||||
|
||||
public String getFullMetric() {
|
||||
return metricMap.entrySet().toString();
|
||||
}
|
||||
|
@ -44,4 +75,34 @@ public class MetricService {
|
|||
public String getStatusMetric() {
|
||||
return statusMetric.entrySet().toString();
|
||||
}
|
||||
|
||||
public Object[][] getGraphData(){
|
||||
final int colCount = statusMetric.keySet().size()+1;
|
||||
final Set<Integer> allStatus = statusMetric.keySet();
|
||||
final int rowCount = timeMap.keySet().size()+1;
|
||||
|
||||
final Object[][] result = new Object[rowCount][colCount];
|
||||
result[0][0] = "Time";
|
||||
|
||||
int j = 1;
|
||||
for (final int status : allStatus) {
|
||||
result[0][j] = status;
|
||||
j++;
|
||||
}
|
||||
int i = 1;
|
||||
HashMap<Integer, Integer> tempMap;
|
||||
for (final Entry<String, HashMap<Integer, Integer>> entry : timeMap.entrySet()) {
|
||||
result[i][0] = entry.getKey();
|
||||
tempMap = entry.getValue();
|
||||
for (j = 1; j < colCount; j++) {
|
||||
result[i][j] = tempMap.get(result[0][j]);
|
||||
if (result[i][j] == null) {
|
||||
result[i][j] = 0;
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>Metric Graph</title>
|
||||
<script
|
||||
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
|
||||
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
|
||||
<script type="text/javascript">
|
||||
google.load("visualization", "1", {
|
||||
packages : [ "corechart" ]
|
||||
});
|
||||
|
||||
function drawChart() {
|
||||
$.get("http://localhost:8080/spring-security-rest-full/metric-graph",
|
||||
function(mydata) {
|
||||
|
||||
var data = google.visualization.arrayToDataTable(mydata);
|
||||
var options = {
|
||||
title : 'Website Metric',
|
||||
hAxis : {
|
||||
title : 'Time',
|
||||
titleTextStyle : {
|
||||
color : '#333'
|
||||
}
|
||||
},
|
||||
vAxis : {
|
||||
minValue : 0
|
||||
}
|
||||
};
|
||||
|
||||
var chart = new google.visualization.AreaChart(document
|
||||
.getElementById('chart_div'));
|
||||
chart.draw(data, options);
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body onload="drawChart()">
|
||||
<div id="chart_div" style="width: 900px; height: 500px;"></div>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue