-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
WebStartView.java
158 lines (128 loc) · 5.88 KB
/
WebStartView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.shiro.samples.spring.ui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.ClassPathResource;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.samples.spring.SampleManager;
/**
* Simple web start application that helps to demo single sign-on and
* remoting authorization using Shiro. The injected <tt>SampleManager</tt>
* is hosted by the Spring sample web application and remotely invoked
* when the buttons in this view are clicked.
*
* @since 0.1
*/
public class WebStartView implements ActionListener, InitializingBean {
/*--------------------------------------------
| C O N S T A N T S |
============================================*/
/*--------------------------------------------
| I N S T A N C E V A R I A B L E S |
============================================*/
private SampleManager sampleManager;
private JTextField valueField;
private JButton saveButton;
private JButton refreshButton;
private JButton secureMethod1Button;
private JButton secureMethod2Button;
private JButton secureMethod3Button;
private JFrame frame;
/*--------------------------------------------
| C O N S T R U C T O R S |
============================================*/
/*--------------------------------------------
| A C C E S S O R S / M O D I F I E R S |
============================================*/
public void setSampleManager(SampleManager sampleManager) {
this.sampleManager = sampleManager;
}
/*--------------------------------------------
| M E T H O D S |
============================================*/
public void afterPropertiesSet() throws Exception {
ClassPathResource resource = new ClassPathResource("logo.png");
ImageIcon icon = new ImageIcon(resource.getURL());
JLabel logo = new JLabel(icon);
valueField = new JTextField(20);
updateValueLabel();
saveButton = new JButton("Save Value");
saveButton.addActionListener(this);
refreshButton = new JButton("Refresh Value");
refreshButton.addActionListener(this);
JPanel valuePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
valuePanel.add(valueField);
valuePanel.add(saveButton);
valuePanel.add(refreshButton);
secureMethod1Button = new JButton("Method #1");
secureMethod1Button.addActionListener(this);
secureMethod2Button = new JButton("Method #2");
secureMethod2Button.addActionListener(this);
secureMethod3Button = new JButton("Method #3");
secureMethod3Button.addActionListener(this);
JPanel methodPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
methodPanel.add(secureMethod1Button);
methodPanel.add(secureMethod2Button);
methodPanel.add(secureMethod3Button);
frame = new JFrame("Apache Shiro Sample Application");
frame.setSize(500, 200);
Container panel = frame.getContentPane();
panel.setLayout(new BorderLayout());
panel.add(logo, BorderLayout.NORTH);
panel.add(valuePanel, BorderLayout.CENTER);
panel.add(methodPanel, BorderLayout.SOUTH);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private void updateValueLabel() {
valueField.setText(sampleManager.getValue());
}
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == saveButton) {
sampleManager.setValue(valueField.getText());
} else if (e.getSource() == refreshButton) {
updateValueLabel();
} else if (e.getSource() == secureMethod1Button) {
sampleManager.secureMethod1();
JOptionPane.showMessageDialog(frame, "Method #1 successfully called.", "Success", JOptionPane.INFORMATION_MESSAGE);
} else if (e.getSource() == secureMethod2Button) {
sampleManager.secureMethod2();
JOptionPane.showMessageDialog(frame, "Method #2 successfully called.", "Success", JOptionPane.INFORMATION_MESSAGE);
} else if (e.getSource() == secureMethod3Button) {
sampleManager.secureMethod3();
JOptionPane.showMessageDialog(frame, "Method #3 successfully called.", "Success", JOptionPane.INFORMATION_MESSAGE);
} else {
throw new RuntimeException("Unexpected action event from source: " + e.getSource());
}
} catch (AuthorizationException ae) {
JOptionPane.showMessageDialog(frame, "Unauthorized to perform action: " + ae.getMessage(), "Unauthorized", JOptionPane.WARNING_MESSAGE);
}
}
}